Socket for connecting simple instances

Source: Internet
Author: User

Http://songchao.blog.51cto.com/133022/37993

Based on Windows Socket socket, simple connection-oriented TCP server and client, instance:

Simple TCP server: Before the server can transmit data to the client, you must do the following: 1: Create a socket; 2: bind the created socket to the local ipendpoint; 3: Set the socket to the listening mode; 4: accept the access connection on the socket. The source program is as follows: The program passes using system debugging in vs2005;
Using system. Collections. Generic;
Using system. Text; using system. net;
Using system. net. Sockets; namespace fixedtcpserver
{
Class Program
{
/// <Summary>
/// Send data
/// </Summary>
/// <Param name = "S"> data sending socket </param>
/// <Param name = "data"> buffer </param>
/// <Returns> </returns>
Private Static int senddata (socket S, byte [] data)
{
Int Total = 0;
Int size = data. length;
Int dataleft = size;
Int sent; while (total <size)
{
Sent = S. Send (data, total, socketflags. None );
Total + = sent;
Dataleft-= sent;
}
Return total;
}
/// <Summary>
/// Accept data
/// </Summary>
/// <Param name = "S"> socket for receiving data </param>
/// <Param name = "size"> </param>
/// <Returns> </returns>
Private Static byte [] receivedata (socket S, int size)
{
Int Total = 0;
Int dataleft = size;
Byte [] DATA = new byte [size];
Int Recv; while (total <size)
{
Recv = S. Receive (data, total, dataleft, 0 );
If (Recv = 0)
{
Data = encoding. ASCII. getbytes ("exit ");
Break;
}
Total + = Recv;
Dataleft-= Recv;
}
Return data;
} Static void main (string [] ARGs)
{Byte [] DATA = new byte [1024]; // defines the buffer
// Define a server
Ipendpoint ipep = new ipendpoint (ipaddresses. Any, 9050 );
Socket newsock = new socket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );
// Bind and start listening
Newsock. BIND (ipep );
Newsock. Listen (10); console. writeline ("waiting for a client ....");
// Accept client connection requests
Socket Client = newsock. Accept (); // use the newly defined socket to receive the return value of the Client Connection Request to output the client's IP address and port information.
Ipendpoint newclient = (ipendpoint) client. remoteendpoint;
Console. writeline ("connected with {0} at Port {1}", newclient. Address, newclient. Port); // send a welcome message to the client
String welcome = "Welcome to my test server ";
Data = encoding. ASCII. getbytes (welcome );
Int sent = senddata (client, data); // call the sending information function for (INT I = 0; I <5; I ++)
{
Data = receivedata (client, 9 );
Console. writeline (encoding. ASCII. getstring (data ));
}
Console. writeline ("disconnected from {0}", newclient. Address );
Client. Close ();
Newsock. Close ();
}
}
} Simple tcp client: based on the preceding TCP server, you can create a simple tcp client to communicate with it. Create a Client Connected to the TCP server as follows: 1: Create a socket; 2: connect the socket to the remote server address. The source code is as follows: This program is successfully debugged in vs2005. Using system;
Using system. Collections. Generic;
Using system. Text; using system. net;
Using system. net. Sockets; namespace fixedtcpclient
{
Class Program
{
Private Static int senddata (socket S, byte [] data)
{
Int Total = 0;
Int size = data. length;
Int dataleft = size;
Int sent; while (total <size)
{
Sent = S. Send (data, total, dataleft, socketflags. None );
Total + = sent;
Dataleft-= sent;
}
Return total;
} Private Static byte [] receivedata (socket S, int size)
{
Int Total = 0;
Int dataleft = size;
Byte [] DATA = new byte [size]; int Recv;
While (total <size)
{
}
} Static void main (string [] ARGs)
{
}
}
}

The above program can be tested on a computer, first open the server, and then open the client. I hope these two programs can help users understand some socket knowledge. For work reasons, only the source code of the two programs is put. For more information, see msdn.

Related Article

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.