Original://http://blog.csdn.net/linybo/article/details/51141960The sample program is a synchronous socket program, the function is very simple, just the client sends a message to the server, the server returns a message to the client, here is just a simple example, is a basic socket programming process, in the following article, The synchronization and asynchrony of sockets are recorded in turn, along with their differences.
first step: ip< Span style= "word-wrap:normal; Word-break:normal; " > establishment of a endpoint
Step two: Establish a socket the image;
step three: bind () method binding
Fourth step: socket listen ()
Fifth step: Accept the connection to the client, socket< Span style= "word-wrap:normal; Word-break:normal; " > accept () ;
Sixth step: After the communication end must remember to close the socket;
Code:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Net;
Using System.Net.Sockets;Namespace Server
{
Class Program
{
static void Main (string[] args)
{
int port = 2000;
String host = "127.0.0.1";
Create an endpoint (EndPoint)
IPAddress IP = ipaddress.parse (host);//Convert IP address string to an instance of IPAddress type
IPEndPoint ipe = new IPEndPoint (IP, port);//Initializes a new instance of the IPEndPoint class with the specified port and IP
/// Create the socket and start listening on
socket s = new socket ( AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);//Create a socket pair image, if using the UDP protocol, You want to use the Sockettype.dgram type socket
s.bind ( IPE);//BIND endpoint to the image (2000 port and IP address)
s. Listen (0);//start listening for
console.writeline ( "Waiting for client Connections");
Accept the client connection, create a new socket for this connection, and accept the information
Socket temp = s.accept ();//Create a new socket for new connection
Console.WriteLine ("Establish connection");
String recvstr = "";
byte[] recvbytes = new byte[1024];
int bytes;
bytes = Temp. Receive (Recvbytes, recvbytes.length, 0);//Receive information from the client
Recvstr + = Encoding.ASCII.GetString (recvbytes, 0, bytes);
Return information to client side
Console.WriteLine ("Server Get Message:{0}", recvstr);//Display the information from the client
String sendstr = "ok! Client Send Message successful! ";
byte[] bs = Encoding.ASCII.GetBytes (SENDSTR);
Temp. Send (BS, BS. Length, 0);//return information to the client
Temp. Close ();
S.close ();
Console.ReadLine ();
}
}
}
Server results:
first step: ip< Span style= "word-wrap:normal; Word-break:normal; " > establishment of a endpoint
Step two: Establish a socket the image;
step three: connect ()
Fourth step: If the connection is successful, use socket< Span style= "word-wrap:normal; Word-break:normal; " > send ()
Fifth step: socket receive () ;
Sixth step: After the communication end must remember to close the socket;
Code:
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Net;
Using System.Net.Sockets; namespace Client
{
class program
{
static void Main (string[] args)
{
try
{
int port = 2000;
string Host = "127.0.0.1"; ///Creating an Endpoint endpoint
ipaddress IP = IPAddress.Parse (host);
// IPAddress IPP = new IPAddress ("127.0.0.1");
ipendpoint Ipe = new IPEndPoint (IP, port);//convert IP and port to IPEndPoint instance
///creating a socket and connecting to the server
socket C = New Socket ( AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);//Create Socket
console.writeline ("Conneting ...");
c.connect ( IPE);//Connect to server
///sending information to the server
string sendstr = "hello! This is a socket test ";
byte[] bs = Encoding.ASCII.GetBytes (SENDSTR);//encode string as Byte
console.writeline ("Send Message");
c.send (BS, Bs. Length, 0);//Send Message
///accept information returned from the server
string recvstr = "";
byte[] Recvbytes = new byte[1024];
int bytes;
bytes = c. Receive (Recvbytes, recvbytes.length, 0);//Receive return information from server
recvstr + = Encoding.ASCII.GetString (recvbytes, 0, bytes);
Console.WriteLine ("Client Get message:{0}", recvstr);//display server return information ///must remember to close the socket when you're done with
c.close (); }
catch (ArgumentNullException e)
{
console.writeline (" ArgumentNullException: {0} ", e);
}
catch (SocketException e)
{
console.writeline ("SocketException:{0}", E);
} }
}
}
Client Side results:
C # Socket programming (the simplest socket communication function)