2013-02-12 13 views
5

In particolare, se un computer ha un server (un'istanza java.net.ServerSocket), posso collegarmi ad esso utilizzando un'istanza C# System.Net.Sockets.Socket?Posso comunicare tra Java e C# usando solo i socket?

+9

Sì. Si riducono alla lettura dei byte, che sono neutrali rispetto alla lingua (meno l'avvertenza divertente che Java non ha 'byte's unsigned come C#). – pickypg

+1

Sì, dai un'occhiata a questa risposta (quella di Blue Gene): http://stackoverflow.com/questions/5999180/sockets-send-strings-from-java-to-c – damix911

+0

@ damix911 Puoi collegare la directory a risposte, ad esempio, [risposta di Blue Gene] (http://stackoverflow.com/a/6001758/438992). –

risposta

23

Il problema principale è che devi essere molto attento con la codifica dei dati che invii e ricevi. Ecco una coppia di programmi che funzionano insieme. Il client C# invia una stringa, inviando prima la lunghezza come numero intero e quindi inviando i byte della stringa stessa. Il server Java legge la lunghezza, quindi legge il messaggio e stampa un output sulla console. Quindi compone un messaggio echo, ne calcola la lunghezza, estrae i byte e lo rimanda al client C#. Il client legge la lunghezza, il messaggio e stampa un'uscita. Ci dovrebbe essere un modo per evitare tutte le cose bitwise, ma onestamente sono un po 'arrugginito con questa roba, specialmente sul lato Java.

server A Java:

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class JavaSocket { 

    public static void main(String[] args) throws IOException { 

     ServerSocket serverSocket = new ServerSocket(4343, 10); 
     Socket socket = serverSocket.accept(); 
     InputStream is = socket.getInputStream(); 
     OutputStream os = socket.getOutputStream(); 

     // Receiving 
     byte[] lenBytes = new byte[4]; 
     is.read(lenBytes, 0, 4); 
     int len = (((lenBytes[3] & 0xff) << 24) | ((lenBytes[2] & 0xff) << 16) | 
        ((lenBytes[1] & 0xff) << 8) | (lenBytes[0] & 0xff)); 
     byte[] receivedBytes = new byte[len]; 
     is.read(receivedBytes, 0, len); 
     String received = new String(receivedBytes, 0, len); 

     System.out.println("Server received: " + received); 

     // Sending 
     String toSend = "Echo: " + received; 
     byte[] toSendBytes = toSend.getBytes(); 
     int toSendLen = toSendBytes.length; 
     byte[] toSendLenBytes = new byte[4]; 
     toSendLenBytes[0] = (byte)(toSendLen & 0xff); 
     toSendLenBytes[1] = (byte)((toSendLen >> 8) & 0xff); 
     toSendLenBytes[2] = (byte)((toSendLen >> 16) & 0xff); 
     toSendLenBytes[3] = (byte)((toSendLen >> 24) & 0xff); 
     os.write(toSendLenBytes); 
     os.write(toSendBytes); 

     socket.close(); 
     serverSocket.close(); 
    } 
} 

Un client C#:

using System; 
using System.Net; 
using System.Net.Sockets; 

namespace CSharpSocket 
{ 
    class MainClass 
    { 
     public static void Main (string[] args) 
     { 
      string toSend = "Hello!"; 

      IPEndPoint serverAddress = new IPEndPoint(IPAddress.Parse("192.168.0.6"), 4343); 

      Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
      clientSocket.Connect(serverAddress); 

      // Sending 
      int toSendLen = System.Text.Encoding.ASCII.GetByteCount(toSend); 
      byte[] toSendBytes = System.Text.Encoding.ASCII.GetBytes(toSend); 
      byte[] toSendLenBytes = System.BitConverter.GetBytes(toSendLen); 
      clientSocket.Send(toSendLenBytes); 
      clientSocket.Send(toSendBytes); 

      // Receiving 
      byte[] rcvLenBytes = new byte[4]; 
      clientSocket.Receive(rcvLenBytes); 
      int rcvLen = System.BitConverter.ToInt32(rcvLenBytes, 0); 
      byte[] rcvBytes = new byte[rcvLen]; 
      clientSocket.Receive(rcvBytes); 
      String rcv = System.Text.Encoding.ASCII.GetString(rcvBytes); 

      Console.WriteLine("Client received: " + rcv); 

      clientSocket.Close(); 
     } 
    } 
} 
+0

gj è funzionante, ha risparmiato un sacco di tempo cercando di capire una storia facile – azuneca

+0

Come spero interessante da parte, Java ha un numero non firmato primitivo, ma è 16 bit, non 8. Il nostro vecchio carattere preferito è un 16 bit senza segno numero dopo tutto Questo è il motivo per cui [Character.toChars (int)] (https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#toChars%28int%29) restituisce una serie di caratteri, a causa dell'intera roba UTF-16. –