插座 - Java客户端,C#服务器 [英] Socket - Java Client, C# Server

查看:99
本文介绍了插座 - Java客户端,C#服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在2个部分做节目。

1部分:在PC上运行C#服务器套接字应用程序,监听命令,并采取相应的行动。

Part 1: C# server-socket Application running on PC, listening for commands, and acts accordingly.

2部分:手机上运行Java客户端插口应用程序,发送命令到PC,当按钮为pressed。

Part 2: Java client-socket application running on phone, that sends a command to the pc, when a button is pressed.

目前,我可以从客户端发送指令到服务器,它都不错。
但我的问题是这样的:当我发送一个特定的命令到服务器,我希望服务器回复给客户端,客户端读取回复

Currently, i can send commands from the client to the server, and its all good. But my problem is this: When i send a specific command to the server, i want the server to reply to the client, and the client to read that reply.

事情恰恰是,当客户端尝试读取,它超时。

Thing just is, when the client tries to read, it time-outs.

Java的客户端程序:

Java client program:

class ClientThread implements Runnable 
{   
    public void run() 
    {
        try 
        {
            Socket socket = new Socket(serverIpAddress, serverPort);
            socket.setSoTimeout(5000);

            while (true) 
            {
                try 
                {
                    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                    Log.d("Nicklas", "Out it goes");
                    out.println(Command);

                    if (Command == "CMD:GetOptions<EOF>")
                    {
                        Log.d("Nicklas", "Getting options");
                        try
                        {
                            Log.d("Nicklas", "Line 1");
                            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                            Log.d("Nicklas", "Line 2");
                            String answer = in.readLine();
                            Log.d("Nicklas", "answer = " + answer );
                        }
                        catch (Exception ee)
                        {
                            Log.d("Nicklasasasas", ee.toString());
                        }
                    }
                    break;

                } 

                catch (Exception e) 
                {
                    Log.d("Nicklas", "CAE = " + e.toString());
                    break;

                } 
            }
            socket.close();
        } 
        catch (ConnectException ee)
        {
            Log.d("Nicklas", "Kunne ikke forbinde");

        }
        catch (Exception e) 
        {
            Log.d("Nicklasssssss", e.toString());
        }
    }
}

这就是所谓的有:

This is called with:

Thread cThread = new Thread(new ClientThread());
        cThread.start();

和使用全局变量命令,其中将包含不同的信息,这取决于是pressed什么按钮。

And uses the global variable "Command", which will contain different information, depending on what button was pressed.

该项目上线失败字符串答案= in.readline();唯独java.net.SocketTimeoutException。

The program fails on the line "String answer = in.readline();" with the exception "java.net.SocketTimeoutException".

这是程序的C#服务器的部分:

This is the C# Server part of the program:

private void ListenForClients()
    {
        this.tcpListener.Start();

        while (true)
        {
            //blocks until a client has connected to the server
            TcpClient client = this.tcpListener.AcceptTcpClient();

            //create a thread to handle communication
            //with connected client
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
            clientThread.Start(client);
        }
    }

    private void HandleClientComm(object client)
    {
        TcpClient tcpClient = (TcpClient)client;
        NetworkStream clientStream = tcpClient.GetStream();

        byte[] message = new byte[4096];
        int bytesRead;

        while (true)
        {
            bytesRead = 0;

            try
            {
                //blocks until a client sends a message
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch
            {
                //a socket error has occured
                break;
            }

            if (bytesRead == 0)
            {
                //the client has disconnected from the server
                break;
            }

            //message has successfully been received
            ASCIIEncoding encoder = new ASCIIEncoding();
            //System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
            string Input = (encoder.GetString(message, 0, bytesRead));
            Input = Input.Trim();
            object[] obj = new object[1];
            obj[0] = Input;
            if (Input == "CMD:GetOptions<EOF>")
            {
                try
                {
                    byte[] buffer = encoder.GetBytes("CMD:Accepted");
                    clientStream.Write(buffer, 0, buffer.Length);
                    clientStream.Flush();
                    MessageBox.Show("Client program asked for reply");
                }
                catch (Exception e)
                {
                    MessageBox.Show("Oh it no work!: " + e.ToString());
                }
            }
            else
            {
                Udfor(Input);
            }


        }

        tcpClient.Close();
    }

与调用下面,在Form1()

Called with the following, in the Form1()

this.tcpListener = new TcpListener(IPAddress.Any, 4532);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();

C#的服务器似乎做工精细,也确实表现在MessageBox要求的回复客户端程序

The C# Server seems to work fine, and does show the messagebox "client program asked for reply"

任何人谁可以发现这个错误吗?

Anyone who can spot the error?

推荐答案

我想它了!
问题是C#。当服务器发回的命令CMD:接受,它永远不会关闭套接字,因此在Android应用程序没有,如果它是读完告诉的想法!关闭套接字再次冲洗当然不会关闭它的+,如果我已经做了之后,没招!

I figured it out! The problem was the C#. When the server sent back the command "CMD:Accepted", it never closed the socket, so the android application had no idea of telling if it was done reading! Closing the socket right after flushing + of course not closing it again if i already did, did the trick!

这篇关于插座 - Java客户端,C#服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆