Basic socket programming of C

Time:2020-1-28

The sample program is a synchronous socket program with simple functions. It only sends a message from the client to the server, and the server returns a message to the client. It is a simple example and also the most basic socket programming process.

Simple steps:

1. Create an endpoint object with the specified port and IP

2. Create a socket object;

3. Bind endpoint with bind() method of socket object

4. Start listening with listen() method of socket object

5. Receive the connection from the client, use the access () method of the socket object to create a new socket object to communicate with the requested client

6. Remember to close socket after communication

Code:

  int port = 2000;
             string host = "127.0.0.1";
             // create endPoint 
             IPAddress ip = IPAddress.Parse(host);
             IPEndPoint ipe = new IPEndPoint(ip, port);
             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
             s.Bind(ipe);
 
             s.Listen(0);
 
             Console.WriteLine("wait a client to connect..");
             Socket temp = s.Accept();
             Console.WriteLine("create a connection");
 
             //receive message
             string recvStr = "";
             byte[] recvBytes= new byte[1024];
             int bytes;
             bytes = temp.Receive(recvBytes, recvBytes.Length, 0);
             recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
             Console.WriteLine("server have get message:{0}",recvStr );
 
             // send message
             string sendStr = "ok! client send message cuccessful";
             byte[] bs = Encoding.ASCII.GetBytes(sendStr);
             temp.Send(bs, bs.Length, 0);
 
             //end
             temp.Close();
             s.Close();
         
             Console.WriteLine("end socket com!");
             Console.ReadLine();

 

 

 

Client:

1. Create an endpoint object with the specified port and IP

2. Create a socket object

3. Send the connection request to the server with the connect() method of socket object and the endpoint object above as the parameter;

4. If the connection is successful, send the information to the server using the send () method of the socket object

5. Use the receive () method of socket object to receive the information sent by the server

6. Remember to close the socket after the communication:

Code::

//init server port and ip
                int port = 2000;
                string host = "127.0.0.1";
                IPAddress ip = IPAddress.Parse(host);

                // create ipendpoint
                IPEndPoint ipe = new IPEndPoint(ip, port);

                // create client sockets
                Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Console.WriteLine("Connecting...");
                
                // connect to server
                c.Connect(ipe);
                
                // send to server
                string sendStr = "Hello, this is a socket test";
                byte[] bs = Encoding.ASCII.GetBytes(sendStr);
                Console.WriteLine("Send Message");
                c.Send(bs, bs.Length, 0);

                // receive a message from server
                string recvStr = "";
                byte[] recvBytes = new byte[1024];
                int bytes;
                bytes = c.Receive(recvBytes, recvBytes.Length, 0);

                recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                Console.WriteLine("Client get message:{0}", recvStr);

                //close Client
                c.Close();

            }
            catch (ArgumentException e)
            {

                Console.WriteLine("argumentNullException:{0}",e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("socketException:{0}",e);
            }
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();

Source: content from the Internet