C#和处理套接字通信

时间:2017-01-17 22:24:57

标签: c# sockets processing

我正在尝试开发一个软件(在C#中),它收集列表中的一些数据(List<string> socketList)。必须将此列表的值发送到处理软件。 在此程序中,服务器(C#软件)等待来自客户端(处理软件)的消息并发送列表的元素。此列表由另一个线程中的方法连续填充(我验证,列表每次都填满)。我使用这种方法是因为我需要每个值都必须到达客户端,无论速度如何。 这里有C#中的方法(它作为线程启动):

public static void InizializeSocket()
{
    //setting the comunication port
    int port = 5000;

    //infinite loop in order to accept sequential clients
    while (true)
    {
        //I open the listener for any IP
        TcpListener listener = new TcpListener(IPAddress.Any, port);
        Console.WriteLine("Listening...");

        //Waiting for a client
        listener.Start();
        TcpClient client = listener.AcceptTcpClient();
        Console.WriteLine("CONNECTED");

        //this string will contain the client message
        String dataReceived = "";

        //loop until "q". If the client send a message with a q, the server disconnect the client
        while (dataReceived != "q")
        {
            //Read the client message
            NetworkStream nwStream = client.GetStream();
            try
            {
                //Define the client message buffer dimension and read the message
                byte[] buffer = new byte[client.ReceiveBufferSize];
                int bytesRead = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
                //Encoding the byte in a string
                dataReceived = Encoding.ASCII.GetString(buffer, 0, bytesRead);

                //check if the List<string> socketList is not empty. If it has any elements, I send the first one and after that I remove the element from the list
                if (socketList.Count > 0)
                {
                    //Encoding the string in a byte
                    byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(socketList[0]);
                    nwStream.Write(bytesToSend, 0, bytesToSend.Length);
                    socketList.RemoveAt(0);
                }
                nwStream.Flush();
            }
            catch (Exception e) { dataReceived = "q"; }
        }
        //Exit from the client loop
        Console.WriteLine("DISCONNECTED");
        client.Close();
        listener.Stop();
    }
}

这里是简单的处理软件

import processing.net.*; 

Client myClient; 
String dataIn;
int port = 5000;
String ip = "127.0.0.1";

void setup () {
size(1100, 1025); 
//Initialize the client
  myClient = new Client(this, "127.0.0.1", 5000);   
}


void draw () {
    //If the client is available it send a generic message to the server and try to receive a response
   if (myClient.available() > 0) { 
    myClient.write("c");
    dataIn = myClient.readString();
  } 
  //print the output in debug
 println(dataIn);
}

此处处理软件能够连接,无论如何,我每次都会收到null

另外,如果我尝试编写(在试用C#软件时):

if (socketList.Count > 0)
{
    byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(socketList[0]);
    nwStream.Write(bytesToSend, 0, bytesToSend.Length);
    socketList.RemoveAt(0);

}

我收到的奇怪值与C#软件中存储的列表中的值不同。

那么,为什么在第一种情况下处理只读null值?为什么在第二种情况下它会读取“随机”值?

编辑: 更好地分析程序流程,我验证了问题所在。我将处理套接字重写为java套接字。它在另一个线程中启动并填充ArrayList。 考虑到代码的简化(为了更好地理解),如果我写:

  Socket s = new Socket("localhost", 5000);
  BufferedReader input =new BufferedReader(new InputStreamReader(s.getInputStream()));
  PrintWriter out=  new PrintWriter(s.getOutputStream(), true);
  out.println("hello");
  String temp = input.readLine();

String temp = input.readLine();上的软件阻止。无论如何,建立通信,我确信C#发送字符串。

0 个答案:

没有答案