简体   繁体   中英

How to receive data sent by client through Socket

So I want to find out how to receive data that the client has sent. I am currently using this code for my connections:

    public void listen()
    {
        TcpListener server = new TcpListener(port);
        server.Start();

        while (true)
        {

            Socket client = server.AcceptSocket();


            connected++;

            client.Close();
        }
    }

How would I go about receiving ASCII data that has been converted to bytes, then reconverting those bytes to ASCII?

You have to create a thread in which you will read the data (or you could use the Asynchronous sockets ) from the connection stream. I have implemented one example of tcp library : https://github.com/alekstheod/Promasi-V2/tree/master/org.promasi.network/src/org/promasi/network/tcp check the tcp client and server classes.

You can use TcpClient instead of Socket while receiving the data using

TcpClient client = server.AcceptTcpClient();  

Code available in MSDN Article: http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener(v=vs.71).aspx

You really should do research / reading on Sockets/Clients interaction in .NET. The code needed to pull of a simple server/client program is more than any single post on here will tell you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM