Lively explanation of TCP-based socket programming

Source: Internet
Author: User

The following are some important knowledge points about tcp-based network programming:

The call process is used to simulate the network transmission of the tcp protocol.

Socket: Like a telephone.

First, we need to install a telephone (loading the socket library). As the server side, we must form a one-to-one relationship with the telephone to receive the telephone (bind ), if you want to receive a call, you must wait for the call (listen to listen). When a call arrives, the server must pick up the call (accept). During this process, when the server learns the details of the client, it receives and sends messages (recv and send), finally puts down the phone (releases the socket), and leaves the phone (releases resources ).

As a client, if you want to call the server and install the same phone number (load the socket Library), you must request the Telecommunications Board to help connect (request to connect). After the Server accepts the request, you can call (send and recv ).

First, we need to understand the steps when using tcp knowledge for programming:

1. programming on the server side:

First, load the socket library. For more information about how to load WSAStartup (), see msdn.

Create a SOCKET: SOCKET creates an object.

Bind to local host and port: bind () function

Listen: listen. This function can set the number of message queues. If the number is exceeded, the request will fail.

When a client request exists, accept () accepts the request.

Then use recv and send for calls.

2. Write the client program:

Load the socket Library first. WSAStartup (), same as above

Request connection: connect ().

When the server establishes a unified connection, you can use recv () to communicate with send.

The following are the programs of a server and a client respectively:

# Include <stdio. h> // note: we have to load the dll in the project, and load the WS2_32.lib library file.

# Include <WINSOCK2.H>

Void main ()

{

WORD wVersionRequested;

WSADATA wsaData;

Int err;

WVersionRequested = MAKEWORD (1, 1 );

Err = WSAStartup (wVersionRequested, & wsaData );

If (err! = 0 ){

Return;

}

If (LOBYTE (wsaData. wVersion )! = 1 |

HIBYTE (wsaData. wVersion )! = 1)

{

WSACleanup ();

Return;

}

SOCKET sockSrv = socket (AF_INET, SOCK_STREAM, 0 );

SOCKADDR_IN addrSrv;

AddrSrv. sin_addr.S_un.S_addr = htonl (INADDR_ANY );

AddrSrv. sin_family = AF_INET;

AddrSrv. sin_port = htons (6000 );

Bind (sockSrv, (SOCKADDR *) & addrSrv, sizeof (SOCKADDR ));

Listen (sockSrv, 5 );

// SOCKET sockClient;

SOCKADDR_IN addrConn;

Int len = sizeof (SOCKADDR );

While (1)

{

SOCKET sockClient = accept (sockSrv, (SOCKADDR *) & addrConn, & len );

Char szBuffer [100];

Sprintf (szBuffer, "welcome % s to http://www.sunxin.org ",

Inet_ntoa (addrConn. sin_addr ));

Send (sockClient, szBuffer, sizeof (szBuffer) + 1, 0 );

Char szRecv [100];

Recv (sockClient, szRecv, 100,0 );

Printf ("% s", szRecv );

}

}

Client Program:

# Include <stdio. h>

# Include <WINSOCK2.H>

Void main ()

{

WORD wVersionRequested;

WSADATA wsaData;

Int err;

WVersionRequested = MAKEWORD (1, 1 );

Err = WSAStartup (wVersionRequested, & wsaData );

If (err! = 0 ){

Return;

}

If (LOBYTE (wsaData. wVersion )! = 1 |

HIBYTE (wsaData. wVersion )! = 1)

{

WSACleanup ();

Return;

}

SOCKET sockClient = socket (AF_INET, SOCK_STREAM, 0 );

SOCKADDR_IN addrSrv;

AddrSrv. sin_addr.S_un.S_addr = inet_addr ("127.0.0.1 ");

AddrSrv. sin_family = AF_INET;

AddrSrv. sin_port = htons (6000 );

Connect (sockClient, (SOCKADDR *) & addrSrv, sizeof (SOCKADDR ));

Char szBuffer [100];

Recv (sockClient, szBuffer, 100,0 );

Printf ("% s", szBuffer );

Send (sockClient, "this is zhangsan", sizeof ("this is zhangsan") + );

}

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.