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

  • Home
  • SEARCH
  • 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 7712563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:39:48+00:00 2026-06-01T01:39:48+00:00

I need a socket communication between my own written java server and C# client,

  • 0

I need a socket communication between my own written java server and C# client, the problem is that the C# client can’t receive any messages from my java server, but sending messages to my java server works.

my workflow:

  1. Java: Create Server
  2. Java: Waiting for Client connection
  3. c#: Create Client
  4. c#: Build connection to the server
  5. c#: send a msg to the server
  6. Java: msg received
  7. java: send msg to c# client
  8. c#: receiving msg from server <- this is the point where the client waits for a message but never get.

Java Server code:

public class Communicator {

   private int m_port;
   private Socket m_socket;
   private ServerSocket m_serverSocket;

   public Communicator(int port) {
        this.m_port = port;
        initConnection();
    }

    private void initConnection() {

        try {
            System.out.println("Creating Server");
            m_serverSocket = new ServerSocket(m_port);
            System.out.println("Waiting for client connection");
            m_socket = m_serverSocket.accept();
            System.out.println("Connection made");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

   public String sendMsg(JSONMessage msg) {
        try {

     //get msg
     BufferedReader bufferedReader = new BufferedReader(
           new InputStreamReader(m_socket.getInputStream()));
     System.out.println("Waiting for msg...");
       String answer = bufferedReader.readLine();
       System.out.println("Received: " + answer);

       //send msg
       PrintWriter writer = new PrintWriter(m_socket.getOutputStream(),true);
       writer.print(msg.getMsg());
       System.out.println("Sending: " + msg.getMsg());

       writer.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }

        return "";
    }

}

My C# client code:

class Communicator
    {
        private int m_port;
        private Thread mainThread;

        public Communicator(int port)
        {
            m_port = port;
            mainThread = new Thread(new ThreadStart(this.initConnection));
            mainThread.Start();
        }

        public void initConnection()
        {
            IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), m_port);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                Console.WriteLine("Trying to build connection");
                server.Connect(ip);
                Console.WriteLine("Connection successful");

                NetworkStream ns = new NetworkStream(server);
                StreamReader sr = new StreamReader(ns);
                StreamWriter sw = new StreamWriter(ns);
                string data;

                string welcome = "Hello";
                Console.WriteLine("Sending: " + welcome);
                sw.WriteLine(welcome);
                sw.Flush();

                Console.WriteLine("Receiving...");
                data = sr.ReadLine();
                // --> NEVER REACHING THIS POINT <---
                Console.WriteLine("Received: " + data);                                         

            }
            catch (SocketException e)
            {
                Console.WriteLine("Connection failed.");
                return;
            }

        }
    }

Does somebody has any idea why it never reaches my client code Console.WriteLine("Received: " + data); ?

I already tried with waits on both sides. I’m not getting any exceptions or error so I don’t have really an idea where my problem is.

Thanks

  • 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-06-01T01:39:50+00:00Added an answer on June 1, 2026 at 1:39 am

    You need to use println() which adds a new line instead of print(). In Java, readLine waits for a new line and I would expect it to do the same in C#. Also println will auto-flush, so you don’t need to flush as well.

    If you intend to use this connection mreo than once, you need to keep the BufferedReader and PrintWriter for the connection. (So I suggest you create these after the socket is created/accepted) Creating these multiple times for the same socket can be error prone and confusing.

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

Sidebar

Related Questions

I need to implement tcp socket communication between server and iOS client. I am
Quick design question: I need to implement a form of communication between a client-server
I need to create 1000 server sockets using Java. Somewhere between creating 600 and
I need to develop a client server system where I can have multiple clients
We are using Java ByteBuffer for socket communication with a C++ server. We know
In my application, we need to have socket communication with the server, and through
I need to maintain a connection between a server and multiple clients so that
I'm trying to write a chat server in C that allows communication between two
I need to provide secure communication between various processes that are using TCP/IP sockets
I need to create a server socket, which should reject a connect request, if

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.