简体   繁体   中英

Java Client C# Server sending issue

I am trying to set up Client-Server communication where C# is the server side and Java is the client side. The connection is successful and I am able to send a string from C# to Java. However, I am having issues sending a string from Java to C#. The Server does not get any input.

My code is the following: Server Side C#

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

namespace ServerToJava
{


    class Program
    {
        static void Main(string[] args)
        {

            TcpListener serverSocket = new TcpListener(5678);
            int requestCount = 0;
            TcpClient clientSocket = default(TcpClient);
            serverSocket.Start();
            Console.WriteLine(" >> Server Started");
            clientSocket = serverSocket.AcceptTcpClient();
            Console.WriteLine(" >> Accept connection from client");

            requestCount = 0;
            NetworkStream networkStream = clientSocket.GetStream();


            try
            {
                requestCount = requestCount + 1;

                string serverResponse = "Last Message from client server This is a test";
                Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                networkStream.Write(sendBytes, 0, sendBytes.Length);          

                Console.Write ("ReadClient Thread started"); 
                int length = networkStream.ReadByte ();
                byte[] buffer = new byte[length];    
                int size = networkStream.ReadByte ();
                byte[] buff = new byte[size];
                  using (var memoryStream = new MemoryStream(buff,false)) {
                    using (var streamReader = new StreamReader(memoryStream, Encoding.UTF8)) {
                var message = streamReader.ReadLine();
                buff = Encoding.UTF8.GetBytes(message);
                networkStream.Read(buffer, 0 ,buffer.Length);
                Console.Write (message);
                networkStream.Flush();
            }

        }

     }
     catch (Exception ex)
    {
    }
    clientSocket.Close();
    serverSocket.Stop();
    Console.WriteLine(" >> exit");
    Console.ReadLine();
        }

    }
}

Client Side Java

 OutputStream out;
            Socket s = new Socket("127.0.0.1", 5678);

            BufferedReader input =
                    new BufferedReader(new InputStreamReader(s.getInputStream()));

            String answer = input.readLine();
            JOptionPane.showMessageDialog(null, answer);
            out= s.getOutputStream();
            out.write('a');
            System.exit(0);

You shouldn't use new TcpListener(5678) it is Obsolete , instead use the following:

 // Set the TcpListener on port 5678.
  Int32 port = 5678;
  IPAddress localAddr = IPAddress.Parse("127.0.0.1");


  TcpListener server = new TcpListener(localAddr, port);

Source` MSDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM