none
How to make a C# chat application using winsock? RRS feed

  • Question

  • I've tried reading some of the tutorials on the internet, but they're too complicated for me (I'm just 14) but I got a few things from them though. This is as far as I got:

    int portNumberVariable = 814; string ipAddressVariable = "127.0.0.1"; // Binding server to 127.0.0.1:814 Console.WriteLine("Binding server to " + ipAddressVariable + ":" + portNumberVariable.ToString() + "..."); SocketPermission socketPermission = new SocketPermission(NetworkAccess.Accept, TransportType.Tcp, ipAddress, portNumber); Socket socketListener; IPHostEntry ipHostEntry = Dns.GetHostEntry(ipAddressVariable); IPAddress ipAddress = ipHostEntry.AddressList[0]; IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, portNumberVariable); socketListener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socketListener.Bind(ipEndPoint); // (This part of the code has been removed by me because it is not relevant for this forum question) .... // Start listening for connections socketListener.Listen(15000);

    Soo, after we make the socket start listening for incoming connections, THEN WHAT? Do we create some sort of 'client connect event'? I need it to be like this:

    1. The server starts

    2. The server binds the socket to 127.0.0.1:814

    3. The server starts listening on that port

    4. A client connects to the server using the address and port (This is a chat application, 15000 clients max)

    5. The client sends it's 'unique ID' to the server

    6. The unique ID gets stored in an array by the server

    7. Rest of the chat stuff codes

    Saturday, February 18, 2017 8:53 AM

All replies

  • There are old tutorials not too complicated from Google.

    Like

    Basic Client/Server Chat Application in C#

    How to C# Chat server programming
    Saturday, February 18, 2017 8:56 AM
  • Hi iNehMan,

    Thank you for posting here.

    According to your question is more related to WCF, I will move it to Windows Communication Foundation, Serialization, and Networking forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 20, 2017 7:02 AM
  • Hi iNehMan,

    Did the suggestion from Castorix31 work for you?

    For server code, I suggest you check below code:

                  public static void StartListening()
            {
                // Data buffer for incoming data.
                byte[] bytes = new Byte[1024];
    
                // Establish the local endpoint for the socket.
                // Dns.GetHostName returns the name of the 
                // host running the application.
                IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                IPAddress ipAddress =ipHostInfo.AddressList[0];
                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 814);
    
                // Create a TCP/IP socket.
                Socket listener = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
    
                // Bind the socket to the local endpoint and 
                // listen for incoming connections.
                try
                {
                    listener.Bind(localEndPoint);
                    listener.Listen(10);
    
                    // Start listening for connections.
                    while (true)
                    {
                        Console.WriteLine("Waiting for a connection...");
                        // Program is suspended while waiting for an incoming connection.
                        Socket handler = listener.Accept();
                        string data = null;
    
                        // An incoming connection needs to be processed.
                        while (true)
                        {
                            bytes = new byte[1024];
                            int bytesRec = handler.Receive(bytes);
                            data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                            if (data.IndexOf("<EOF>") > -1)
                            {
                                break;
                            }
                        }
    
                        // Show the data on the console.
                        Console.WriteLine("Text received : {0}", data);
    
                        // Echo the data back to the client.
                        byte[] msg = Encoding.ASCII.GetBytes(data);
    
                        handler.Send(msg);
                        handler.Shutdown(SocketShutdown.Both);
                        handler.Close();
                    }
    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
    
                Console.WriteLine("\nPress ENTER to continue...");
                Console.Read();
    
            }
    

    For client code, I suggest you refer below code:

            public static void StartClient()
            {
                // Data buffer for incoming data.
                byte[] bytes = new byte[1024];
    
                // Connect to a remote device.
                try
                {
                    // Establish the remote endpoint for the socket.
                    // This example uses port 11000 on the local computer.
                    IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
                    //IPAddress ipAddress = ipHostInfo.AddressList[0];
                    //IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
                    //IPAddress ipAddress = IPAddress.Parse("127.0.0.1"); //ipHostInfo.AddressList[0];
                    IPAddress ipAddress = IPAddress.Parse("10.168.197.122");
                    IPEndPoint remoteEP = new IPEndPoint(ipAddress, 814);
    
    
                    // Create a TCP/IP  socket.
                    Socket sender = new Socket(AddressFamily.InterNetwork,
                        SocketType.Stream, ProtocolType.Tcp);
    
                    // Connect the socket to the remote endpoint. Catch any errors.
                    try
                    {
                        sender.Connect(remoteEP);
    
                        Console.WriteLine("Socket connected to {0}",
                            sender.RemoteEndPoint.ToString());
    
                        // Encode the data string into a byte array.
                        byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>");
    
                        // Send the data through the socket.
                        int bytesSent = sender.Send(msg);
    
                        // Receive the response from the remote device.
                        int bytesRec = sender.Receive(bytes);
                        Console.WriteLine("Echoed test = {0}",
                            Encoding.ASCII.GetString(bytes, 0, bytesRec));
    
                        // Release the socket.
                        sender.Shutdown(SocketShutdown.Both);
                        sender.Close();
    
                    }
                    catch (ArgumentNullException ane)
                    {
                        Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
                    }
                    catch (SocketException se)
                    {
                        Console.WriteLine("SocketException : {0}", se.ToString());
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Unexpected exception : {0}", e.ToString());
                    }
    
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
    

    If there is any issue, please feel free to let us know.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 20, 2017 7:19 AM