Reputation: 3319
This is my problem, all the messages sent from the c# client aren't received by the server until that I Shutdown the socket on client side and finally the server receive all data in once time.
c# client side
public static class AsynchronousClient
{
// The port number for the remote device.
private const int port = 8888;
// ManualResetEvent instances signal completion.
private static ManualResetEvent connectDone =
new ManualResetEvent(false);
private static ManualResetEvent sendDone =
new ManualResetEvent(false);
private static ManualResetEvent receiveDone =
new ManualResetEvent(false);
public static Socket client;
// The response from the remote device.
private static String response = String.Empty;
public static void StartClient()
{
// Connect to a remote device.
try
{
// Establish the remote endpoint for the socket.
// The name of the
// remote device is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.GetHostEntry("127.0.0.1");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 8888);
// Create a TCP/IP socket.
client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Connect to the remote endpoint.
client.BeginConnect(remoteEP,
new AsyncCallback(ConnectCallback), client);
connectDone.WaitOne();
// Send test data to the remote device.
Send(client, "test connection");
sentDown.WaitOne();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void ConnectCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)ar.AsyncState;
// Complete the connection.
client.EndConnect(ar);
Console.WriteLine("Socket connected to {0}",
client.RemoteEndPoint.ToString());
// Signal that the connection has been made.
connectDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public static void Send(Socket client, String data)
{
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, SocketFlags.None,
new AsyncCallback(SendCallback), null);
}
public static void SendCallback(IAsyncResult ar)
{
try
{
// Retrieve the socket from the state object.
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(ar);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);
// Signal that all bytes have been sent.
sendDone.Set();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Java server side
public class Connection_to_port extends Thread {
final static int port = 8888;
private Socket socket;
String message = "";
public static void main(String[] args) {
try {
ServerSocket socketServeur = new ServerSocket(port);
while (true) {
Socket socketClient = socketServeur.accept();
Connection_to_port t = new Connection_to_port(socketClient);
t.start();
System.out.println("Connected to client : " + socketClient.getInetAddress());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Connection_to_port(Socket socket) {
this.socket = socket;
}
public void run() {
handleMessage();
}
public void handleMessage() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
message = in.readLine();
System.out.println(message);
}
catch (Exception e) {
e.printStackTrace();
}
}
In my client i have to send some data to the server like that
AsynchronousClient.Send(AsynchronousClient.client, "{myjsondata}");
My client is just for sending, not receive.
The problem is, my java server receive nothing ! But the client said it's sent, and i see on Wireshark that's it's send.
When i do
AsynchronousClient.client.Shutdown(SocketShutdown.Both);
Finally i see all my message on the server at the same time. Like if i sent only one message.
Upvotes: 1
Views: 1689
Reputation: 34638
The Java side is trying to read a line (you are using readLine
).
This method will not return until either there is an end-of-line character, or the stream is closed.
When you shutdown the client, in effect, the stream closes.
Your test message does not include an end-of-line character, so the only way for readLine
to return is at the end of stream.
Upvotes: 2
Reputation: 12196
When you write to a socket, the message does not sent, it's saved in buffer until:
Try the following methods:
Upvotes: 1