C # Socket simple example

Source: Internet
Author: User

Socket, also known as Socket, is divided into connection-oriented and connectionless sockets. They have their own processes in use.

Connection orientation:

Server

1 instantiate socket 2bind local endpoint 3listen listen 4accept accept connection 5accept return new socket 6 new socket receive method revenue cache and send Method send to client 7shutdown method release connection 8 close socket

Client

 1 instantiate socket 2connect establish connection 3 new socket receive method revenue cache and send Method send to server side 4shutdown method release connection 5 close socket

 

 

Connection-free:

Server

 1 instantiate socket 2bind local endpoint 3 new socket receive method revenue cache and send method sent to client 4shutdown method release connection 5 close socket

Client

 1 instantiate socket 2bind local endpoint 3 new socket receive method revenue cache and send method sent to server side 4shutdown method release connection 5 close socket

 

 

Use code to implement the Socket simple example of the console application

The server code is as follows:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Net;
Using System. Net. Sockets;
Using System. IO;
Using System. Threading;

Namespace socketsample
{
Class Program
{
Static void Main (string [] args)
{
IPHostEntry iphost = Dns. GetHostByName (Dns. GetHostName ());
IPAddress ip = iphost. AddressList [0];
// IPAddress ip = IPAddress. Parse ("192.168.0.101 ");
Console. WriteLine (ip. ToString () + ": 8088 ");
Host h = new Host ();
H. SocketStrat (ip, 8088 );

}
}

Public class Host
{
Socket tsocket;
Public void SocketStrat (IPAddress ip, int port)
{
Try
{
IPEndPoint iep = new IPEndPoint (ip, port );
Tsocket = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );
// Socket usocket = new Socket (AddressFamily. InterNetwork, SocketType. Dgram, ProtocolType. Udp );
Tsocket. Bind (iep );
While (true)
{
Tsocket. Listen (0 );
Socket newSocket = tsocket. Accept ();
String teststring = "";
Byte [] testbyte = new byte [1, 1024];
Int bytes;
Bytes = newSocket. Receive (testbyte, testbyte. Length, 0 );
Teststring + = Encoding. ASCII. GetString (testbyte, 0, bytes );
Console. WriteLine (teststring );
String sendStr = "OK! Sucess! ";
Byte [] bs = Encoding. ASCII. GetBytes (sendStr );
// NewSocket. Send (bs, bs. Length, 0 );
EndPoint ep = newSocket. RemoteEndPoint;
NewSocket. SendTo (bs, ep );
NewSocket. Shutdown (SocketShutdown. Both );
NewSocket. Close ();
}
Tsocket. Close ();
}
Catch (SocketException se)
{
Console. WriteLine (se. ToString ());
}
Catch (ArgumentNullException AE)
{
Console. WriteLine (AE. ToString ());
}
Console. ReadLine ();
}

}
}

 

 

The client code is as follows:

 

 

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Net;
Using System. Net. Sockets;
Using System. Threading;
Using System. IO;

Namespace Client
{
Class Program
{
Static void Main (string [] args)
{
Try
{

IPAddress ip = IPAddress. Parse ("192.168.1.118 ");
IPEndPoint iep = new IPEndPoint (ip, 8088 );
Socket s = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );
S. Connect (iep );
String str = "hello! This is a socket test ";
Byte [] tbs = Encoding. ASCII. GetBytes (str );
S. Send (tbs, tbs. Length, 0 );

String recvstr = "";
Byte [] rbs = new byte [1024];
S. Receive (rbs, rbs. Length, 0 );
Recvstr + = Encoding. ASCII. GetString (rbs );
Console. WriteLine (recvstr );
S. Shutdown (SocketShutdown. Both );
S. Close ();
}
Catch (ArgumentNullException e)
{
Console. WriteLine ("ArgumentNullException: {0}", e );
}
Catch (SocketException e)
{
Console. WriteLine ("SocketException: {0}", e );
}

Console. ReadLine ();

}
}
}

 

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.