I have a socket server and try to receive a string from client.
Client is perfect and when I use this
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte b = new byte;
int k = s.Receive(b);
for (int i = 0; i < k; i++)
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
string action = Convert.ToChar(b[i]);
The Name i isn't in the current context.
its the only Error Message i get.
s is a
Socket object on which you call receive, you get an
byte back. To convert this back to a string, use the appropiate encoding, e.g.
string szReceived = Encoding.ASCII.GetString(b);
Edit: Since the buffer
b is always 100 bytes, but the actual number of bytes received varies with each connection, one should use the return value of the
Socket.Receive() call to only convert the actual number of received bytes.
byte b = new byte; int k = s.Receive(b); string szReceived = Encoding.ASCII.GetString(b,0,k);