Reputation: 5312
I'm currently trying to learn network connections with C#, and I went right ahead and started with sockets.
I read up some guides now, and I found a somewhat good and understandable guide, but now they supply me with this code for a serversocket, and I have some struggle understanding certain passages
Code:
public void startServer()
{
TcpListener serverSocket = new TcpListener(8888);
TcpClient clientSocket = default(TcpClient);
serverSocket.Start();
Console.WriteLine(" >> Server Started");
clientSocket = serverSocket.AcceptTcpClient();
Console.WriteLine(" >> Accept connection from client");
while ((true))
{
try
{
NetworkStream networkStream = clientSocket.GetStream();
byte[] bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
Console.WriteLine(" >> Data from client - " + dataFromClient);
string serverResponse = "Last Message from client" + dataFromClient;
Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
networkStream.Flush();
Console.WriteLine(" >> " + serverResponse);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
clientSocket.Close();
serverSocket.Stop();
Console.WriteLine(" >> exit");
Console.ReadLine();
}
First of all, it's pretty obvious what is happening at the beginning. A TcpListener is being created, which is representing the server socket.
However, confusion somewhat starts at the TcpClient part. If this is supposed to be a server, why do I have to create a client here already? Shouldn't server and client methods be separated, considering that the server should be running standalone?
Then, in the try catch, why is the NetworkStream = clientsocket.GetStream();
? I might be utterly wrong here, but why do I have to get the stream from my client? This line sounds like the server is expecting a regular update. Or is the information / message to transport being stored in the client, until the server "collects" it?
The byte array is supposed to store a message, that's what I'm pretty sure of.
I can't really say a lot about the other statements in the try block, as long as I'm still confused by the other parts. But in general, it looks like the server is establishing a connection to a client here, and transmitting/receiving messages bidirectional.
I'm not really advanced in network or socket techniques (not at all to be honest, I just started with it), so I'm sorry if I make some really false assumptions here, but I'm really trying to get this snippet of code without any explanation.
If someone could clear me up on this code, I'd be very thankful.
Upvotes: 0
Views: 62
Reputation: 3082
Looks like a mixed solution for me.
Typically TcpListener listener = new TcpListener(8888)
awaits for the connection on given port.
Then, when it accepts the connection from client, it establishes connection on different socket Socket socket = listener.AcceptSocket()
so that listening port remains awaiting for other connections (another clients).
Then, we can read from client that connected to server from the stream: Stream stream = NetworkStream(socket)
.
TcpClient
is used to connect to such server and it should be used in Client application, not in server one.
Upvotes: 1
Reputation: 34421
The term client has two definitions. At the application level you have a client and server application. The client is the master and the server is the slave. At the socket level, both the client application and server application have a client (also called a socket). The server socket listens at the loopback address 127.0.0.1 (or IPAny). While the client socket connects to the server IP address.
Upvotes: 0