Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket - Java Client, C# Server

I am making a program in 2 parts.

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

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 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();

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

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

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();
    }

Called with the following, in the Form1()

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

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

Anyone who can spot the error?

like image 211
user1285334 Avatar asked Jun 22 '12 10:06

user1285334


People also ask

What does socket () do in C?

The socket() function shall create an unbound socket in a communications domain, and return a file descriptor that can be used in later function calls that operate on sockets. The socket() function takes the following arguments: domain. Specifies the communications domain in which a socket is to be created.

What is client socket java?

A socket is one endpoint of a two-way communication link between two programs running on the network. The socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.

What is difference between socket () and ServerSocket () class?

Here, two classes are being used: Socket and ServerSocket. The Socket class is used to communicate client and server. Through this class, we can read and write message. The ServerSocket class is used at server-side.


1 Answers

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!

like image 141
user1285334 Avatar answered Oct 02 '22 10:10

user1285334