Socket self-study Note 1

Source: Internet
Author: User
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 ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.