这是我的问题,服务器不接收从c#客户端发送的所有消息,直到我在客户端关闭套接字,最后服务器一次性接收所有数据。
c#客户端
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服务器端
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();
}
}
在我的客户端,我必须将一些数据发送到服务器
AsynchronousClient.Send(AsynchronousClient.client, "{myjsondata}");
我的客户只是发送而不是接收。
问题是,我的java服务器什么都没收到!但客户说它已发送,我在Wireshark上看到它是发送的。
当我这样做时 AsynchronousClient.client.Shutdown(SocketShutdown.Both);
最后,我同时在服务器上看到了我的所有消息。就像我只发送一条消息一样。
答案 0 :(得分:2)
Java方正在尝试读取一行(您正在使用readLine
)。
在有行尾字符或流已关闭之前,此方法不会返回。
关闭客户端时,实际上会关闭流。
您的测试消息不包含行尾字符,因此readLine
返回的唯一方法是在流的末尾。
答案 1 :(得分:1)
当您写入套接字时,消息不会被发送,它会保存在缓冲区中,直到:
尝试以下方法: