Socket Communication C #

Source: Internet
Author: User

1, through the socket to receive and send data on the network, the equivalent of an API, with it. No need to operate the NIC directly

2, using the socket of some basic conditions: (1) Determine the local IP and port, socket only with a certain IP and port binding, to play a strong power (2) protocol TCP UDP

3, with the basic conditions, you can use the socket to access the network step: (1) Establish a socket (2) Bind the IP and port (3) If it is TCP, because it is connection-oriented, So use the Listeno () method to listen to whether someone is sending something to themselves on the network (TCP is responsible for discovering the problem of transmission, signaling it when there is a problem, requesting retransmission until all data is safely and correctly transmitted to the destination. IP is a single address for every networked device on the Internet. ); If it is UDP, because it is not connected, all-in-one (TCP) Transmission Control Protocol, is a general agreement that provides reliable data transfer. (UDP) User Datagram Protocol, which is a non-connection oriented protocol. Using this protocol does not require two applications to establish a connection first. The UDP protocol does not provide error recovery and cannot provide data retransmission, so the protocol transmits data with poor security. )

4, TCP, if the supervisor hears a connection, you can use accept to receive the connection, and then you can use send/receive to perform the operation. UDP, however, does not need to accept, directly using Sendto/receivefrom to perform the operation. (See Clearly, and TCP execution method is different, because UDP does not need to establish a connection, so before sending the other side of the IP and port, so you need to specify a sending node for normal send and receive)

5, if you do not want to continue to send and receive, do not waste resources. Close if you can close.

Connection-oriented:

Using System;
Using System.Net;
Using System.Net.Sockets;
Using System.Text;
Namespace TCPServer
{
<summary>
A summary description of the CLASS1.
</summary>
Class Server
{
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main (string[] args)
{
//
TODO: Add code here to launch the application
//
int recv;//is used to indicate the length of information sent by the client
Byte[] data=new byte[1024];//used to cache the information sent by the client, the information passed through the socket must be a byte array
IPEndPoint ipep=new IPEndPoint (ipaddress.any,9050);//native pre-use IP and port
Socket newsock=new socket (ADDRESSFAMILY.INTERNETWORK,SOCKETTYPE.STREAM,PROTOCOLTYPE.TCP);
Newsock. Bind (IPEP);//Bind
Newsock. Listen (10);//monitoring
Console.WriteLine ("Waiting for a Client");
Socket Client=newsock. Accept ();//When there is an available client connection attempt to execute and return a new socket for communication with the client
IPEndPoint clientip= (ipendpoint) client. Remoteendpoint;
Console.WriteLine ("Connect with client:" +clientip. Address+ "at Port:" +clientip. Port);
String Welcome= "Welcome here!";
Data=encoding.ascii.getbytes (welcome);
Client. Send (Data,data. Length,socketflags.none);//Send Message
while (true)
{//Use a dead loop to constantly get information from the client
Data=new byte[1024];
Recv=client. Receive (data);
Console.WriteLine ("recv=" +recv);
if (recv==0)//When the message length is 0, the client connection is disconnected
Break
Console.WriteLine (Encoding.ASCII.GetString (DATA,0,RECV));
Client. Send (Data,recv,socketflags.none);
}
Console.WriteLine ("Disconnected from" +clientip. Address);
Client. Close ();
Newsock. Close ();

}
}
}

No connection-oriented

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Net;
Using System.Net.Sockets;
Namespace Simpleudpsrvr
{
Class Program
{
static void Main (string[] args)
{
int recv;
byte[] data = new byte[1024];
IPEndPoint Ipep = new IPEndPoint (Ipaddress.any, 9050);//define a network endpoint
Socket Newsock = new socket (addressfamily.internetwork, Sockettype.dgram, PROTOCOLTYPE.UDP);//define a socket
Newsock. Bind (IPEP);//socket is associated with a local endpoint
Console.WriteLine ("Waiting for a Client:");

IPEndPoint sender = new IPEndPoint (ipaddress.any, 0);//Defines the address of the computer to be sent
EndPoint Remote = (EndPoint) (sender);//
recv = Newsock. ReceiveFrom (data, ref Remote);//Accept
Console.WriteLine ("Message Received from{0}:", remote.tostring ());
Console.WriteLine (Encoding.ASCII.GetBytes (DATA,0,RECV));

String welcome = "Welcome to my test server!";
data = Encoding.ASCII.GetBytes (welcome);
Newsock. SendTo (data, data. Length, Socketflags.none, Remote);
while (true)
{
data = new byte[1024];
recv = Newsock. ReceiveFrom (data, ref Remote);
Console.WriteLine (Encoding.ASCII.GetString (data, 0, recv));
Newsock. SendTo (data, recv, Socketflags.none, Remote);
}
}
}
}

Socket Communication C #

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.