Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7051031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:12:55+00:00 2026-05-28T03:12:55+00:00

I’m writting a client(Android) – server(c#) application. I get the code from here: How

  • 0

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 !!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:12:56+00:00Added an answer on May 28, 2026 at 3:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.