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?
risposta
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();
}
}
}
gj è funzionante, ha risparmiato un sacco di tempo cercando di capire una storia facile – azuneca
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. –
- 1. Come comunicare tra Python e C# usando XML-RPC?
- 2. Come posso comunicare tra thread
- 3. Il modo più veloce per comunicare tra C++ e C#
- 4. Come posso comunicare tra un Siemens S7-1200 e Python?
- 5. Come comunicare tra i servizi Windows
- 6. Java TCP Socket Sniffing
- 7. Socket - Client Java, server C#
- 8. Differenze su Java Socket tra Windows e Linux - Come gestirli?
- 9. Java Socket e ServerSocket Problema
- 10. Come creare un socket Java che sia solo localhost?
- 11. Come posso verificare se gunicorn funziona e comunicare con nginx?
- 12. Programmazione endianness e socket in C
- 13. Passare i puntatori tra C e Java attraverso JNI
- 14. Come comunicare tra Android e Unity?
- 15. socket Java 7 e TLSv1.2
- 16. Come comunicare usando l'iframe tra 2 app angolari?
- 17. Comunicazione socket, client Java C server
- 18. Possibili modi per comunicare tra iFrame e la pagina padre tra i domini
- 19. Grafica usando solo standard C++?
- 20. I socket sono integrati nello standard C++?
- 21. Client di posta in Python usando solo socket (senza smtplib)
- 22. java socket writeUTF() e readUTF()
- 23. differenza tra i valori() e solo()
- 24. Come comunicare tra php e boost della libreria IPC?
- 25. trasferimento doppio tra java e simulink usando udp
- 26. C# Il modo migliore per comunicare tra le classi
- 27. C socket socket client/server
- 28. Differenze tra C# asincrona e Java ExecutorService
- 29. Java: timeout timeout socket socket
- 30. Differenza tra C# utilizzando e l'importazione Java
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
Sì, dai un'occhiata a questa risposta (quella di Blue Gene): http://stackoverflow.com/questions/5999180/sockets-send-strings-from-java-to-c – damix911
@ damix911 Puoi collegare la directory a risposte, ad esempio, [risposta di Blue Gene] (http://stackoverflow.com/a/6001758/438992). –