I just want to know how the proper way of doing 2 way connection with one socket is (C#).
I need the client sending and also receiving data from the server without to open the port on the client-pc / router.
The server will be a multiplayer game server and the clients should not extra open a port to play the game.
So does a simple socket connection work in 2 ways (server has a socket listener, and client connects to the server socket)?
Hope that text pretty much explains my question.
Yes, the client can only connect to the port. Then, the Server may respond back to the client's connection

Example
Client
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
try
{
server.Connect(ip); //Connect to the server
} catch (SocketException e){
Console.WriteLine("Unable to connect to server.");
return;
}
Console.WriteLine("Type 'exit' to exit.");
while(true)
{
string input = Console.ReadLine();
if (input == "exit")
break;
server.Send(Encoding.ASCII.GetBytes(input)); //Encode from user's input, send the data
byte[] data = new byte[1024];
int receivedDataLength = server.Receive(data); //Wait for the data
string stringData = Encoding.ASCII.GetString(data, 0, receivedDataLength); //Decode the data received
Console.WriteLine(stringData); //Write the data on the screen
}
server.Shutdown(SocketShutdown.Both);
server.Close();
This will allow the client to send data to the server. Then, wait for the response from the server. However, if the server does not respond back the client will hang on for much time.
Here's an example from the server
IPEndPoint ip = new IPEndPoint(IPAddress.Any,9999); //Any IPAddress that connects to the server on any port
Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp); //Initialize a new Socket
socket.Bind(ip); //Bind to the client's IP
socket.Listen(10); //Listen for maximum 10 connections
Console.WriteLine("Waiting for a client...");
Socket client = socket.Accept();
IPEndPoint clientep =(IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}",clientep.Address, clientep.Port);
string welcome = "Welcome"; //This is the data we we'll respond with
byte[] data = new byte[1024];
data = Encoding.ASCII.GetBytes(welcome); //Encode the data
client.Send(data, data.Length,SocketFlags.None); //Send the data to the client
Console.WriteLine("Disconnected from {0}",clientep.Address);
client.Close(); //Close Client
socket.Close(); //Close socket
This will allow the server to send a response back to the client upon the client's connection.
Thanks,
I hope you find this helpful :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With