繁体   English   中英

套接字问题c#客户端到java服务器

[英]Socket issue c# client to java server

这是我的问题,服务器不接收从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);

最后,我同时在服务器上看到了我的所有消息。 就像我只发送一条消息一样。

Java端正在尝试读取一行(您正在使用readLine )。

在存在行尾字符或流已关闭之前,此方法不会返回。

当您关闭客户端时,实际上流将关闭。

您的测试消息不包含行尾字符,因此readLine返回的唯一方法是在流的末尾。

当您写入套接字时,消息不会被发送,它会保存在缓冲区中,直到:

  • 缓冲区已满
  • 您请求清理/刷新缓冲区
  • 你关机了。

请尝试以下方法:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM