Due to the experiment in the TCP/IP principle course, this time I carefully studied Socket programming and compared something modeled. This time I used C # To fulfill the basic requirements of the teacher, this is also required for the software competition, so I will continue to study it in depth.
First, I made a server, installed the socket step, and carried out simple message acceptance and message reply. Although there is already a ready-made code on the internet, I tried it again ......
Sever code:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Net;
Using System. Net. Sockets;
Using System. IO;
Namespace server
{
Class Program
{
Static void Main (string [] args)
{
Try
{
Int port = 2000;
String host = "127.0.0.1 ";
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 ("waite for connect ");
Socket temp = s. Accept ();
String recvStr = "";
Byte [] recvBytes = new byte [1, 1024];
Int bytes;
Bytes = temp. Receive (recvBytes, recvBytes. Length, 0 );
RecvStr + = Encoding. Unicode. GetString (recvBytes, 0, bytes );
Console. WriteLine ("Get Message {0}", recvStr );
String sendStr = "Your Message:" + recvStr;
Byte [] bs = Encoding. Unicode. GetBytes (sendStr );
Temp. Send (bs, bs. Length, 0 );
Temp. Close ();
S. Close ();
}
Catch (ArgumentNullException e)
{
Console. WriteLine ("ArgumentNullException: {0}", e );
}
Catch (SocketException e)
{
Console. WriteLine ("SocketException:", e );
}
Console. WriteLine ("Press Enter to Exit ");
Console. ReadLine ();
}
}
}
The client side is similar. In order to display Chinese characters, the unicode mode is selected:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. IO;
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 ";
IPAddress ip = IPAddress. Parse (host );
IPEndPoint ipe = new IPEndPoint (ip, port );
Socket c = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );
Console. WriteLine ("Connecting ");
C. Connect (ipe );
String sendStr = "Someone is a genius! ";
Byte [] bs = Encoding. Unicode. GetBytes (sendStr );
Console. WriteLine ("Sending Message ");
C. Send (bs, bs. Length, 0 );
String recvStr = "";
Byte [] recvBytes = new byte [1, 1024];
Int bytes;
Bytes = c. Receive (recvBytes, recvBytes. Length, 0 );
RecvStr + = Encoding. Unicode. GetString (recvBytes, 0, bytes );
Console. WriteLine ("Client Get Message: {0}", recvStr );
C. Close ();
}
Catch (ArgumentNullException e)
{
Console. WriteLine ("ArgumentNullException: {0}", e );
}
Catch (SocketException e)
{
Console. WriteLine ("SocketException: {0}", e );
}
Console. WriteLine ("Press Enter to Exit ");
Console. ReadLine ();
}
}
}
This is only the first step, but it can be further improved to add more functions ......