3

I write code like in this article. My code:

static void Main() { TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 8080); server.Start(); Console.WriteLine("Server has started on 127.0.0.1:8080"); Console.WriteLine("Waiting for a connection..."); TcpClient client = server.AcceptTcpClient(); Console.Write("A client connected."); NetworkStream stream = client.GetStream(); //enter to an infinite cycle to be able to handle every change in stream while (true) { while (!stream.DataAvailable) ; byte[] bytes = new byte[client.Available]; stream.Read(bytes, 0, bytes.Length); //translate bytes of request to string string request = Encoding.UTF8.GetString(bytes); if (new Regex("^GET").IsMatch(request)) { byte[] response = UTF8.GetBytes("HTTP/1.1 101 Switching Protocols" + Environment.NewLine + "Connection: Upgrade" + Environment.NewLine + "Upgrade: websocket" + Environment.NewLine + "Sec-WebSocket-Accept: " + Convert.ToBase64String( SHA1.Create().ComputeHash( Encoding.UTF8.GetBytes( new Regex("Sec-WebSocket-Key: (.*)").Match(request).Groups[1].Value.Trim() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" ) ) ) + Environment.NewLine + Environment.NewLine); stream.Write(response, 0, response.Length); } else { byte[] response = UTF8.GetBytes("Data received"); response[0] = 0x81; // denotes this is the final message and it is in text response[1] = (byte)(response.Length - 2); // payload size = message - header size stream.Write(response, 0, response.Length); } } } 

I try debug to see it work, but it runs to the line

TcpClient client = server.AcceptTcpClient(); 

and stops. It shows Waiting for a connection... and stops.

3
  • Well, did you try to connect to it? AcceptTcpClient blocks the thread until something actually connects. Try navigating to http://localhost:8281 in your web browser. Commented Mar 10, 2015 at 3:28
  • Wow! I tried and it worked. Thank you very much. #Blorgbeard, I want send a string: "Hello word", Can you help me? Commented Mar 10, 2015 at 3:44
  • It seems that you have not a much experience with C#. I would suggest to start with a simpler task than this. Then come back in few months. Commented Mar 10, 2015 at 3:57

1 Answer 1

3

As #Blorgbeard mentioned should have client as well, you can check this code https://github.com/sta/websocket-sharp

Or Simple code like this:

var tcpclnt = new TcpClient(); Console.WriteLine("Connecting....."); tcpclnt.Connect(IPAddress.Parse("127.0.0.1"), 8080); // use the ipaddress as in the server program Console.WriteLine("Connected"); Console.Write("Enter the string to be transmitted : "); String str = Console.ReadLine(); Stream stm = tcpclnt.GetStream(); ASCIIEncoding asen = new ASCIIEncoding(); byte[] ba = asen.GetBytes(str); Console.WriteLine("Transmitting....."); stm.Write(ba, 0, ba.Length); byte[] bb = new byte[100]; int k = stm.Read(bb, 0, 100); for (int i = 0; i < k; i++) Console.Write(Convert.ToChar(bb[i])); tcpclnt.Close(); 
Sign up to request clarification or add additional context in comments.

3 Comments

#Peyman I tried run code it. Error in picture following : i.imgur.com/0AXL6vD.png
Have you created Console Application project?
#Peyman I run code:github.com/sta/websocket-sharp. It show error bellow. I run code follow "Or Simple code like this:", It show error line: tcpclnt.Connect(IPAddress); Error: "System.Net.IPAddress" is type but is used like a 'variable'. If I delete tcpclnt.Connect(IPAddress), it show error: "An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll Additional information: The operation is not allowed on non-connected sockets." in line Stream stm = tcpclnt.GetStream()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.