1

I'm writting a client(Android) - server(c#) application. I get the code from here: How to make client on Android listen to server on C#? Everythings is working fine, when i just send message from the client to server, and from server to client (closing the socket on server side). Now, what i want is : send message to server, receive message from server, then send again a message to server. The server hangs at sending the message. If i close the socket on server side after sending, it gives a dispose error, and i can's send the data from the server.

My server code is:

/*************************************SERVER*****************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace SERVER2
{
    class Program
    {
        public static void Main()
        {
            try
            {
                IPAddress ipAd = IPAddress.Parse("192.168.2.102");

                TcpListener myList = new TcpListener(ipAd, 18001);

                myList.Start();

                Console.WriteLine("The server is running at port 18001...");
                Console.WriteLine("The local End point is  :" +
                                  myList.LocalEndpoint);
                Console.WriteLine("Waiting for a connection.....");
                 m:
                Socket s = myList.AcceptSocket();
                Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

                byte[] b = new byte[100];
                int k = s.Receive(b);

                char cc = ' ';
                string test = null;
                Console.WriteLine("Recieved1...");
                for (int i = 0; i < k - 1; i++)
                {
                    cc = Convert.ToChar(b[i]);
                    test += cc.ToString();
                }
                Console.WriteLine("Received characters1: "+test);

                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes("The string was recieved by the server."));
                Console.WriteLine("\nSent Acknowledgement");
                //s.Close(); <-if i enable this, i get a dispose error

                k = s.Receive(b);//i get dispose error here

                cc = ' ';
                test = null;
                Console.WriteLine("Recieved2...");
                for (int i = 0; i < k - 1; i++)
                {
                    cc = Convert.ToChar(b[i]);
                    test += cc.ToString();
                }
                Console.WriteLine("Received characters2: " + test);

                /* clean up */
                goto m;
                s.Close();
                myList.Stop();

            }
            catch (Exception e)
            {
                Console.WriteLine("Error..... " + e.Message);
            }
            Console.ReadLine();
        }

    }
}

My client code is:

/******************************CLIENT*****************************************/
    Socket socket = null;
        try 
        {
            Toast.makeText(context,"IP: "+ip+" port: "+port,10000).show();
            InetAddress serverAddr = InetAddress.getByName(ip); 
            socket = new Socket(serverAddr, port);
        } 
        catch (UnknownHostException e1) 
        {
            Toast.makeText(context,"UnknownHostException ",10000).show();
        } 
        catch (IOException e1) 
        {
            Toast.makeText(context,"IOException ",10000).show();
        }

        String message = "1";

        PrintWriter out = null;
        BufferedReader in = null;

        try { 
            Log.d("TCP", "C: Sending: '" + message + "'"); 

            /*write*/
            out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
            out.println(message);
            out.flush();
            /*read*/
            in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
            String text = "";
            String finalText = "";
            while ((text = in.readLine()) != null) 
            {
                finalText += text;
                }
            Toast.makeText(context, "FinalText: "+finalText, 10000).show();
            Log.d("TCP", "C: Sent.");
            Log.d("TCP", "C: Done."); 
            in.close();        

            /*write*/
            out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
            out.println(message);
            out.flush();
        } catch(Exception e) { 
            Log.e("TCP", "S: Error", e); 
        } finally { 
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        } 

Thanks advanced !!!

Community
  • 1
  • 1
DevtoLoper
  • 49
  • 1
  • 12

3 Answers3

0

You should use the stream of the tcp client.. an example can be found here: http://msdn.microsoft.com/de-de/library/system.net.sockets.tcplistener.aspx

And don't use goto, please. You will never reach the end lines after goto m; Surround the code with a while loop which checks if an timeout is occured or any other checks.

The reason why the server hangs is that he waits for 100 bytes to receive.

Felix C
  • 1,755
  • 5
  • 26
  • 40
  • i tried to use streams like you said, but the when i send the message, it hangs at this part: stream.Write(msg, 0, msg.Length); and nothing gets written out on the client side. If i add a stream.close() i get a disposed error at the while loop, but i get the message from server. – DevtoLoper Jan 06 '12 at 14:08
0

The reason you get the dispose error is because the s.close() closes the socket, and then your next s.Receive() is trying to read from a closed socket.

Also the hang you are seeing might be caused by in.close(); in your java code. It could be closing the underlying socket. Try commenting it out and see if that fixes your hanging problem.

Not an expert in C#, but I've done my share of socket programming.

Basically what you want is 1 + n threads.

One thread that just accepts connections.

For each socket that is returned by Socket s = myList.AcceptSocket(); you want a thread for sending/receiving data and processing the messages from the socket. You could also use two threads (one that sends, one that receives (this is the asynchronous case)).

TheBigS
  • 491
  • 1
  • 4
  • 14
  • i tried to comment out the in.close() part, but without luck. The app still hangs at send. Where should i put the Socket s = myList.AcceptSocket(); ? – DevtoLoper Jan 06 '12 at 13:56
0

The problem was that when the client received the message from the server , in the while part entered into an infinite loop. I modify my app like this:

My client part:

try 
        {
            Toast.makeText(context,"IP: "+ip+" port: "+port,10000).show();
            InetAddress serverAddr = InetAddress.getByName(ip); 
            socket = new Socket(serverAddr, port);
        } 
        catch (UnknownHostException e1) 
        {
            Toast.makeText(context,"UnknownHostException ",10000).show();
        } 
        catch (IOException e1) 
        {
            Toast.makeText(context,"IOException ",10000).show();
        }

        String message = "HELLO FROM CLIENT";

        PrintWriter out = null;
        BufferedReader in = null;

        try { 
            Log.d("TCP", "C: Sending: '" + message + "'"); 

            /*write*/
            OutputStream ostr=socket.getOutputStream();
            OutputStreamWriter outputstr=new OutputStreamWriter(ostr);
            BufferedWriter buffw=new BufferedWriter(outputstr);
            out = new PrintWriter(buffw ,true);
            out.println("HELLO 1 FROM CLIENT");

            /*read - i modify to this*/
            InputStreamReader reader=new InputStreamReader(socket.getInputStream());
            char[] bytesreceived=new char[50];
            reader.read(bytesreceived    , 0, 50);
            String text="";
            for (int i=0;i<bytesreceived.length;i++)
            {
                text+=bytesreceived[i];
            }
            Toast.makeText(context, "Received1: "+text.trim(), 10000).show();
            Log.d("IdealLog","Received1: "+text.trim());

            /*write*/
            out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
            out.println("HELLO 2 FROM CLIENT");

            /*read*/
            reader=new InputStreamReader(socket.getInputStream());
            bytesreceived=new char[50];
            reader.read(bytesreceived    , 0, 50);
            text="";
            for (int i=0;i<bytesreceived.length;i++)
            {
                text+=bytesreceived[i];
            }
            Toast.makeText(context, "Received2: "+text.trim(), 10000).show();
            Log.d("IdealLog","Received2: "+text.trim());

        } catch(Exception e) { 
            Log.e("TCP", "S: Error", e); 
        } finally { 
            try {
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
        } 

My server side code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace SocketServer
{
    class Program
    {
        static void Main(string[] args)
        {
            IPEndPoint ip = new IPEndPoint(IPAddress.Any, 18001);
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Bind(ip);
            socket.Listen(10);
            Console.WriteLine("Waiting for a client...");
            Socket client = socket.Accept();
            IPEndPoint clientep = (IPEndPoint)client.RemoteEndPoint;
            Console.WriteLine("Connected with {0} at port {1}", clientep.Address, clientep.Port);



            string welcome = "HELLO 1 FROM SERVER";
            byte[] data = new byte[200];
            int receiveddata=client.Receive(data);
            Console.WriteLine("Received data from CLIENT1: {0}", System.Text.ASCIIEncoding.ASCII.GetString(data).Trim());



            ASCIIEncoding asen = new ASCIIEncoding();
            byte[] data2 = new byte[200];
            data2 = asen.GetBytes(welcome);
            int sentdata=client.Send(data2, data2.Length, SocketFlags.None);
            Console.WriteLine("Sent data from SERVER: {0}", welcome);



            byte[] data3 = new byte[200];
            Console.WriteLine("Receiving data from CLIENT : {0}", "...");
            client.Receive(data3);


            Console.WriteLine("Received data from CLIENT2: {0}", System.Text.ASCIIEncoding.ASCII.GetString(data3).Trim());
            byte[] data4 = new byte[200];
            data4 = asen.GetBytes("HELLO 2 FROM SERVER");
            sentdata = client.Send(data4, data4.Length, SocketFlags.None);

            client.Close();
            socket.Close();


            Console.WriteLine("Disconnected from {0}", clientep.Address);



            Console.ReadLine();
        }
    }
}

Now, everything is working fine, without hang. The only problem is, that i don't know if this will work for receiving , sending files.

DevtoLoper
  • 49
  • 1
  • 12