I want to create a UDP socket to receive data from Local Endpoint.
I do not know the Remote Port where data come from, that's why I thought I would use ReceiveAsync. But it does not work.
I give my code right now and any advice would be useful:
public class Program
{
ManualResetEvent clientDone;
Socket socket;
public static void Main(string[] args)
{
clientDone = new ManualResetEvent(false);
socket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint localIPEP = new IPEndPoint( IPAddress.Any, 0);
socket.Bind( (EndPoint) localIPEP);
while (listening)
Receive();
}
string Receive()
{
string response;
byte[] recvData = new byte[24];
if (socket != null)
{
SocketAsyncEventArgs ae = new SocketAsyncEventArgs();
ae.SetBuffer(recvData, 0, recvData.Length);
// callback
ae.Completed += new EventHandler<SocketAsyncEventArgs>(delegate (object s, SocketAsyncEventArgs e)
{
if (e.SocketError == SocketError.Success)
{
response = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
response.Trim('\0');
}
else
{
response = e.SocketError.ToString();
}
switch (e.LastOperation)
{
case SocketAsyncOperation.Receive:
ProcessReceivedData(e);
break;
}
clientDone.Set();
});
clientDone.Reset();
Console.WriteLine("Local EndPoint: " + ((IPEndPoint)socket.LocalEndPoint).ToString());
socket.ReceiveAsync(ae);
clientDone.WaitOne(1000);
}
return response;
}
}
P.S. I work on Linux .Net Core
Related
Attempting to jump into the Windows Server ServiceBus 1.1 code-base along with adopting the new TPL async methods. But I could not find an easy way to just spin up N number of handlers for message sessions (might have 100 or so concurrent sessions). So it would be great to get some feedback on the following code, any suggestions on an easier way would be great... note tried to keep code sample simple for the questions purpose.
///example usage
SubscriptionClient subClient = SubscriptionClient.Create(TopicName, SubscriptionName);
subClient.HandleSessions(5, msg =>
{
Console.WriteLine(string.Format("Processing recived Message: SessionId = {0}, Body = {1}",
msg.SessionId,
msg.GetBody<string>()));
msg.Complete();
});
public static class SubscriptionClientExtensions
{
public static void HandleSessions (this SubscriptionClient sc, Int32 numberOfSessions, Action<BrokeredMessage> handler)
{
Action<Task<MessageSession>> sessionAction = null;
Action<Task<BrokeredMessage>> msgHandler = null;
sessionAction = new Action<Task<MessageSession>>(tMS =>
{
if (tMS.IsFaulted) // session timed out - repeat
{
sc.AcceptMessageSessionAsync().ContinueWith(sessionAction);
return;
}
MessageSession msgSession = null;
try
{
msgSession = tMS.Result;
}
catch (Exception)
{
return; // task cancelation exception
}
msgHandler = new Action<Task<BrokeredMessage>>(taskBM =>
{
if (taskBM.IsFaulted)
return;
BrokeredMessage bMsg = null;
try
{
bMsg = taskBM.Result;
}
catch (Exception)
{
return; // task cancelation exception
}
if (bMsg == null)
{
sc.AcceptMessageSessionAsync().ContinueWith(sessionAction); // session is dead
return;
}
handler(bMsg); // client code to handle the message
msgSession.ReceiveAsync(TimeSpan.FromSeconds(5)).ContinueWith(msgHandler); // repeat
});
msgSession.ReceiveAsync(TimeSpan.FromSeconds(5)).ContinueWith(msgHandler); // start listening
});
for (Int32 nIndex = 0; nIndex < numberOfSessions; nIndex++)
{
sc.AcceptMessageSessionAsync().ContinueWith(sessionAction);
}
}
}
This is the code for my client and server.
class Client1
{
Client1(int no)
{
try
{
String message;
message="Hello this is client "+no;
byte[] b =message.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length,InetAddress.getLocalHost(),3700);
DatagramSocket sender = new DatagramSocket();
sender.send(dp);
}catch (Exception e)
{
System.out.println("client shutdown");
}
}
}
Then my server class is
class Server1
{
int cnt=0;
String s1;
Server1()
{
try {
byte[] buffer = new byte[65536];
DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);
DatagramSocket ds = new DatagramSocket(3700);
ds.receive(incoming);
byte[] data = incoming.getData();
String s = new String(data, 0, incoming.getLength());
System.out.println("Port" + incoming.getPort() + " on " + incoming.getAddress() + " sent this message:");
System.out.println(s.toUpperCase());
}
catch (IOException e)
{
System.err.println(e);
}
}
}
Then my runnable implementation is
class prothread implements Runnable {
//long time=0;
//int portno;
int flag=0; // this is to differentiate between a server and client
private String capitalizedSentence;
prothread(long l)
{
if(l==1)
{ // it is a server
flag=1;
}
else
{
flag=(int) l;
}
}
#Override
public void run(){
// TODO Auto-generated method stub
System.out.println("Starting thread");
if(flag==1)// Code for server
{
Server1 s=new Server1();
}
else // code for client
{
Client1 c=new Client1(flag);
}
}
}
Finally the class which deploys this client and server is
public class Samplepro31 {
public static void main(String[] args) {
// First i'm going to create a server and then clients for it
int i=1;
int cnt=0;
prothread[] p;
Thread[] th;
Random r =new Random();
// Array has been declared
p=new prothread[10];// Memory allocated to it
th= new Thread[1000];
p[0]=new prothread(1);
cnt=1;
//p[0].setportno(cnt);
th[0]=new Thread(p[0]);
th[0].start();
while(cnt<3)
{
p[cnt]=new prothread(cnt);
// here send the port number
th[cnt]=new Thread(p[cnt]);
//p[cnt1].setportno(cnt1);
th[cnt].start();
cnt++;
}
}
}
So problem I'm having is one server and only one client is running at a time
instead 2 clients should be running the o/p i'm getting is :
Starting thread
Starting thread
Starting thread
Inside clinet's constructor 2
java.net.BindException: Address already in use: Cannot bind
HELLO THIS IS CLIENT 2
So can anybody tell me what I'm doing wrong?
Don't bind the client to any particular port. Let the implementation select an available port to bind to.
I am trying to do communication between a mobile application (using J2ME and JSR82) and Desktop application (in C# using InTheHand Library).
I am using RFComm protocol with UUID: 00000003-0000-1000-8000-00805f9b34fb.
I have manually specified uuid on both devices. I have mobile application to wait for incoming connections, while desktop application sends data to it.
But, my mobile application just does not listen to incoming connection. It just hangs at the message: "Waiting for incoming connection..."
J2ME code in Mobile Application:
public void startApp() {
if (midletPaused) {
resumeMIDlet();
} else {
initialize();
startMIDlet();
form.append("UID: "+ uuid.toString() +"\n");
//set the device discoverable
try {
LocalDevice localDevice = LocalDevice.getLocalDevice();
localDevice.setDiscoverable(DiscoveryAgent.GIAC);
form.append("Device Address: "+localDevice.getBluetoothAddress()+"\n");
form.append("Name: "+ localDevice.getFriendlyName()+"\n");
}
catch (BluetoothStateException exception) {
form.append(exception.toString()+"\n");
}
//setup a server socket
StreamConnectionNotifier streamConnectionNotifier = null;
try {
String url = "btspp://localhost:000300001000800000805f9b34fb;name=rfcommtest;authorize=true";
//form.append(url);
streamConnectionNotifier = (StreamConnectionNotifier)Connector.open(url);
if (streamConnectionNotifier == null) {
form.append("Error: streamConnectionNotifier is null\n");
return;
}
}
catch (Exception exception) {
form.append(exception.toString()+"\n");
}
//wait for an incoming connection
StreamConnection streamConnection = null;
try {
form.append("Waiting for incoming connection...\n");
streamConnection = streamConnectionNotifier.acceptAndOpen();
if (streamConnection == null) {
form.append("Error: streamConnection is null\n");
} else {
form.append("Connection received.\n");
}
}
catch (Exception exception) {
form.append(exception.toString()+"\n");
}
//write hello and then exit
try {
OutputStream out = streamConnection.openOutputStream();
form.append("Stream \n");
String s = "hello";
out.write(s.getBytes());
out.flush();
streamConnection.close();
form.append("Text Written to stream\n");
}
catch (Exception exception) {
form.append(exception.toString()+"\n");
}
}
midletPaused = false;
}
C# Code in Desktop App:
cli = new BluetoothClient();
BluetoothEndPoint ep1 = new BluetoothEndPoint(info[listBox1.SelectedIndex].DeviceAddress, BluetoothService.RFCommProtocol);
cli.Connect(ep1);
Stream stream = cli.GetStream();
StreamWriter sw = new StreamWriter(stream);
sw.WriteLine("Tesing");
sw.WriteLine("testing");
sw.Flush();
sw.Close();
stream.Close();
Please help me out on this.
I'm trying to implement a simple TCP connection between Client/Server. I made the Server multithreaded so that it can take either multiple requests (such as finding the sum, max, min of a string of numbers provided by the user) from a single client or accept multiple connections from different clients. I'm running both of them on my machine, but the server doesn't seem to push out an answer. Not sure what I'm doing wrong here --
public final class CalClient {
static final int PORT_NUMBER = 6789;
public static void main (String arg[]) throws Exception
{
String serverName;
#SuppressWarnings("unused")
String strListOfNumbers = null;
int menuIndex;
boolean exit = false;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter host name...");
System.out.print("> ");
serverName = inFromUser.readLine();
Socket clientSocket = new Socket(serverName, PORT_NUMBER);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//outToServer.writeBytes(serverName + '\n');
System.out.println("");
System.out.println("Enter 1 to enter the list of numbers");
System.out.println("Enter 2 to perform Summation");
System.out.println("Enter 3 to calculate Maximum");
System.out.println("Enter 4 to calculate Minimum");
System.out.println("Enter 5 to Exit");
while (!exit) {
System.out.print(">");
menuIndex = Integer.parseInt(inFromUser.readLine());
if (menuIndex == 1) {
System.out.println("Please enter the numbers separated by commas.");
System.out.print(">");
strListOfNumbers = inFromUser.readLine();
outToServer.writeBytes("List" + strListOfNumbers);
//continue;
}
else if (menuIndex == 2) {
outToServer.writeBytes("SUM");
System.out.println(inFromServer.readLine());
}
else if (menuIndex == 3) {
outToServer.writeBytes("MAX");
System.out.println(inFromServer.readLine());
}
else if (menuIndex == 4) {
outToServer.writeBytes("MIN");
System.out.println(inFromServer.readLine());
}
else if (menuIndex == 5) {
outToServer.writeBytes("EXIT");
exit = true;
}
}
}
}
public final class CalServer
{
static final int PORT_NUMBER = 6789;
public static void main(String[] args) throws IOException
{
try {
ServerSocket welcomeSocket = new ServerSocket(PORT_NUMBER);
System.out.println("Listening");
while (true) {
Socket connectionSocket = welcomeSocket.accept();
if (connectionSocket != null) {
CalRequest request = new CalRequest(connectionSocket);
Thread thread = new Thread(request);
thread.start();
}
}
} catch (IOException ioe) {
System.out.println("IOException on socket listen: " + ioe);
ioe.printStackTrace();
}
}
}
final class CalRequest implements Runnable
{
Socket socket;
BufferedReader inFromClient;
DataOutputStream outToClient;
TreeSet<Integer> numbers = new TreeSet<Integer>();
int sum = 0;
public CalRequest(Socket socket)
{
this.socket = socket;
}
#Override
public void run()
{
try {
inFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outToClient = new DataOutputStream(socket.getOutputStream());
while(inFromClient.readLine()!= null) {
processRequest(inFromClient.readLine());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void processRequest(String string) throws IOException
{
String strAction = string.substring(0,3);
if (strAction.equals("LIS")) {
String strNumbers = string.substring(5);
String[] strNumberArr;
strNumberArr = strNumbers.split(",");
// convert each element of the string array to type Integer and add it to a treeSet container.
for (int i=0; i<strNumberArr.length; i++)
numbers.add(new Integer(Integer.parseInt(strNumberArr[i])));
}
else if (strAction.equals("SUM")) {
#SuppressWarnings("rawtypes")
Iterator it = numbers.iterator();
int total = 0;
while (it.hasNext()) {
total += (Integer)(it.next());
}
}
else if (strAction.equals("MAX")) {
outToClient.writeBytes("The max is: " + Integer.toString(numbers.last()));
}
else if (strAction.equals("MIN")) {
outToClient.writeBytes("The max is: " + Integer.toString(numbers.first()));
}
}
}
Since you are using readLine(), I would guess that you actually need to send line terminators.
My experience with TCP socket communications uses ASCII data exclusively, and my code reflects that I believe. If that's the case for you, you may want to try this:
First, try instantiating your data streams like this:
socket = new Socket (Dest, Port);
toServer = new PrintWriter (socket.getOutputStream(), true);
fromServer = new BufferedReader (new InputStreamReader
(socket.getInputStream()), 8000);
The true at the end the printWriter constructor tells it to auto flush (lovely term) the buffer when you issue a println.
When you actually use the socket, use the following:
toServer.println (msg.trim());
resp = fromServer.readLine().trim();
I don't have to append the \n to the outgoing text myself, but this may be related to my specific situation (more on that below). The incoming data needs to have a \n at its end or readLine doesn't work. I assume there are ways you could read from the socket byte by byte, but also that the code would not be nearly so simple.
Unfortunately, the TCP server I'm communicating with is a C++ program so the way we ensure the \n is present in the incoming data isn't going to work for you (And may not be needed in the outgoing data).
Finally, if it helps, I built my code based on this web example:
http://content.gpwiki.org/index.php/Java:Tutorials:Simple_TCP_Networking
Edit: I found another code example that uses DataOutputStream... You may find it helpful, assuming you haven't already seen it.
http://systembash.com/content/a-simple-java-tcp-server-and-tcp-client/
I created a basic tcp client and server in groovy and I'm wanting to send maps from the server to the client, I'm wondering if I'm able send maps across and still being able to access the values.
//TCP Server
def book1 = [Title of Book: "Groovy Recipes", Author: "Scott Davis", Number of Pages: "241"]
server = new ServerSocket(2000)
println("Waiting for connection")
while(true) {
server.accept() { socket ->
socket.withStreams { input, output ->
w = new BufferedWriter(new OutputStreamWriter(output))
String message = "Connection was successful"
r = new BufferedReader(new InputStreamReader(input))
while(true) {
if(message != null) {
w.writeLine(message)
w.flush()
message = null
}
String a = r.readLine()
if(a=="book1") {
message = book1
} else {
message = "$a command unknown."
sendMessage(message)
println message
message = null
}
}
}
}
}
def sendMessage(String msg) {
try {
w.writeLine(msg)
w.flush();
} catch(IOException ioException) {
ioException.printStackTrace();
}
}
Here is my Client (where I'm wanting to receive the map and get the values)
//TCP Client
def grabBookInfo {
queryData()
}
public void queryData() {
def hosts = ["localhost"]
for(int aHost = 0; aHost < hosts.size; aHost++) {
bookClient(hosts[aHost]);
}
}
public void bookClient() {
def commands = ["book1"]
def answers = [commands.size]
def requestSocket = new Socket(host, 2000)
r = new BufferedReader(new InputStreamReader(requestSocket.getInputStream()));
w = new BufferedWriter(new OutputStreamWriter(requestSocket.getOutputStream()));
String message = "Connection was successful"
message = r.readLine()
println("Server>" + message)
for(int n = 0; n < commands.size; n++) {
sendMessage(commands[n]);
answers[n] = r.readLine()
}
//get map values here
//answers[0] = Book
//println Book.['Title of Book']
//println Book.['Author']
//println Book.['Number of Pages']
w.flush()
w.close()
}
public void sendMessage(msg) {
w.write(msg+"\r\n");
w.flush();
System.out.println("client>" + msg);
}
}
Am I on the right track?
In the server, use ObjectOutputStream.
In the client use ObjectInputStream.
Server:
private static final HashMap<String, Integer> TEST_MAP;
static {
TEST_MAP = new HashMap<String, Integer>();
TEST_MAP.put("one", 1);
TEST_MAP.put("two", 2);
}
...
ServerSocket ss = new ServerSocket(port);
Socket socket = ss.accept();
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeObject(TEST_MAP);
out.close();
Client:
Socket socket = new Socket(HOST, PORT);
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
Object o = in.readObject();
assert o instanceof HashMap<?, ?>;
#SuppressWarnings("unchecked")
HashMap<String, Integer> m = (HashMap<String, Integer>)o;
assertTrue(m.get("one") == 1);
assertTrue(m.get("two") == 2);
in.close();
socket.close();
Serialize the maps, example
http://www.java2s.com/Tutorial/Java/0140__Collections/SerializingHashMaps.htm