2 way socket between applications/devices

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dave750
    New Member
    • Sep 2011
    • 9

    #1

    2 way socket between applications/devices

    I am new to C# and am writing a program that will run on a pc and allow either another program or an external device, like a touchpanel control system, to connect to it. I have the rest of my code ready, but I can't figure out how to make the socket connection work.
    I need the following

    My application listens for a connection.
    Device connects to my application.
    the socket stays open until it is disconnected by the device.
    The application must be able to send to or receive from the device at any time, full duplex. Neither operation can block or depend on the other.

    This seems like something that would be common, but I can't seem to find a good enough example or thread here on Bytes to get me going in the right direction. Can anyone hep?
  • Dave750
    New Member
    • Sep 2011
    • 9

    #2
    After I posted, I figured it out - and, of course, it wasn't that hard. For anyone that got stuck like me, it is below. Any issues with doing it this way?


    Code:
        class Com
        {
            TcpListener tcpListener = new TcpListener(IPAddress.Loopback, 50000);
            TcpClient tcpClient;
            NetworkStream clientStream;
    
            public Com()
            {
                tcpListener.Start();
                tcpClient = tcpListener.AcceptTcpClient();
                clientStream = tcpClient.GetStream();
                Thread Reader = new Thread(new ThreadStart(SocketRead));
                Reader.IsBackground = true;
                Reader.Start();
                Reader.Join();
            }
    
            private void SocketSend(string data)
            {
    
                try
                {
                    ASCIIEncoding encoder = new ASCIIEncoding();
                    byte[] message = encoder.GetBytes(data);
                    clientStream.Write(message, 0, message.Length);
                    clientStream.Flush();
                    Console.WriteLine(data + " sent");
                }
                catch
                {
    
                }
                return;
            }
    
            private void SocketRead()
            {
                bool listening = true;
                while (listening)
                {
                    byte[] message = new byte[4096];
                    int bytesRead;
                    while (true)
                    {
                        bytesRead = 0;
                        try
                        {
                            bytesRead = clientStream.Read(message, 0, 4096);
                        }
                        catch
                        {
                            break;
                        }
    
                        if (bytesRead == 0)
                        {
                            break;
                        }
                        ASCIIEncoding encoder = new ASCIIEncoding();
                        string received = encoder.GetString(message, 0, bytesRead);
                        Console.WriteLine(received + " received");
                        Thread Sender = new Thread(() => SocketSend(received));
                        Sender.Start();
                    }
                }
            }
        }

    and an easy client for it in Python

    Code:
    import socket
    
    class Client:
        def __init__(self):
            self.Sender()
    
        def Sender(self):
            host = 'localhost'
            port = 50000
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            s.connect((host, port))
            
            while True:
                data = raw_input("Send to Server>> ")
                s.send(data)
                data = s.recv(1024)
                if data == "exit":
                    break
                print data
            s.close()
    
    Client()

    Comment

    Working...