Vale, y éste socket en C# qué es TCP/IP? UDP? En qué puerto está?
Voy a hacerte un ejemplo de Cliente - Servidor en Java y ahora edito
Edit:
Éste código usa sockets que se conectan mediante TCP/IP. Ésto quiere decir que cuando un servidor recibe un mensaje de un socket, ésta conexión queda abierta hasta que alguno de las 2 partes la cierra, o hasta que se interrumpa la conexión. Con éstos sockets tienes la seguridad de que el mensaje que envias llegará casi con toda seguridad.
Si quieres usar UDP tendrás que usar DatagramSocket.
El código Servidor es:
package test.serverclient;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.net.ServerSocketFactory;
public class Server {
ServerSocket serverSocket;
public Server(int port) {
try {
System.out.println("CREATING SERVER SOCKET ON PORT "+port);
serverSocket = ServerSocketFactory.getDefault().createServerSocket(port);
System.out.println("SERVER SOCKET CREATED!!!11one");
} catch (IOException e) {
System.err.println("#ERROR# COULD NOT START SERVER AT PORT "+port);
}
}
private Socket waitForConnection() {
Socket socket = null;
try {
socket = serverSocket != null ? serverSocket.accept() : null;
} catch (Exception e) {
System.err.println("#ERROR# ERROR WAITING FOR CONNECTIONS Problem:"+e.getMessage());
}
return socket;
}
public Message<?> receiveMessage(){
Socket s = waitForConnection();
if (s == null) { return null;}
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(s.getInputStream());
Object o = ois.readObject();
if (o instanceof Message<?>){
return (Message)o;
}
} catch (Exception e) {
System.err.println("#ERROR# Problem reading input stream data:"+e.getMessage());
}finally{
if (ois != null){
try {ois.close();}catch (IOException e) {}
}
try {s.close();}catch (IOException e) {}
}
return null;
}
public void closeSocket(){
try {
if (this.serverSocket != null){
this.serverSocket.close();
}
} catch (IOException e) {
System.err.println("#ERROR# Problem closing Server Socket:"+e.getMessage());
}
}
}
El código Cliente es
package test.serverclient;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.net.SocketFactory;
public class Client {
private Socket socket = null;
public Client(String host,int port){
try {
this.socket = SocketFactory.getDefault().createSocket(host, port);
} catch (UnknownHostException e) {
System.err.println("#ERROR# Host could not be resolved");
} catch (IOException e) {
System.err.println("#ERROR# Problem creating socket");
}
}
public boolean sendMessage(Message<?> m){
if (socket == null) { return false; }
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
System.err.println("#ERROR# Problem creating stream:"+e.getMessage());
return false;
}
try {
oos.writeObject(m);
} catch (IOException e) {
System.err.println("#ERROR# Problem sending message:"+e.getMessage());
return false;
}
return true;
}
}
Código de una clase para enviar mensajes
package test.serverclient;
import java.io.Serializable;
public class Message<T> implements Serializable{
private static final long serialVersionUID = -6003784135450805116L;
private T contents;
public Message(){
}
public Message(T contents) {
this.contents = contents;
}
public T getContents() {
return contents;
}
public void setContents(T contents) {
this.contents = contents;
}
}
Código de tests
Test Server
package test.serverclient;
public class TestServer {
/**
* @param args
*/
public static void main(String[] args) {
Server s = new Server(9090);
Message<?> m = s.receiveMessage();
System.out.println("RECEIVED! "+m.getContents());
}
}
Test Client
package test.serverclient;
public class TestClient {
public static void main(String[] args) {
Client c = new Client("localhost", 9090);
boolean sended = c.sendMessage(new Message<String>("HELLO SOCKET WORLD!"));
if (sended){
System.out.println("MESSAGE SENDED SUCCESSFULLY!");
}
}
}