Socket programming steps

Source: Internet
Author: User
Tags htons
There are three types of socket programming: stream socket (sock_stream), datagram socket (sock_dgram), and original socket (sock_raw). TCP-based socket programming is a streaming socket.
In this program, add two projects to a work zone. Link a library file of ws2_32.lib.


Server programming steps:

1: load the socket library and create a socket (wsastartup ()/socket ());

2: bind a socket to an IP address and a port (BIND ());

3: Set the socket to listening mode and wait for the connection request (Listen ());

4: after the request arrives, accept the connection request and return a new socket (accept () corresponding to the connection ());

5: Use the returned socket to communicate with the client (send ()/Recv ());

6: return. Wait for another connection request;

7. Close the socket and the loaded socket Library (closesocket ()/wsacleanup ()).

The server code is as follows:
# Pragma comment (Lib, "ws2_32.lib") // This sentence is very important. No compilation error occurs.
# Include <stdio. h>
# Include <winsock2.h>
Void main ()
{
Word wversionrequested;
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 );

Sockaddr_in addrclient;
Int Len = sizeof (sockaddr );
While (1)
{
Socket sockconn = accept (socksrv, (sockaddr *) & addrclient, & Len );
Char sendbuf [50];
Sprintf (sendbuf, "Welcome % s to here! ", Inet_ntoa (addrclient. sin_addr ));
Send (sockconn, sendbuf, strlen (sendbuf) + 1, 0 );
Char recvbuf [50];
Recv (sockconn, recvbuf, 50, 0 );
Printf ("% s \ n", recvbuf );
Closesocket (sockconn );
}
}

Client programming steps:

1: load the socket library and create a socket (wsastartup ()/socket ());

2: Send a connection request (connect () to the server ());

3: communicate with the server (send ()/Recv ());

4: Close the socket and close the loaded socket Library (closesocket ()/wsacleanup ()).

The client code is as follows:

# Pragma comment (Lib, "ws2_32.lib") // This sentence is very important. No compilation error occurs.
# Include <stdio. h>
# Include <winsock2.h>
Void main ()
{
Word wversionrequested;
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 ));
Send (sockclient, "hello", strlen ("hello") + 1, 0 );
Char recvbuf [50];
Recv (sockclient, recvbuf, 50, 0 );
Printf ("% s \ n", recvbuf );

Closesocket (sockclient );
Wsacleanup ();
}

Run the following command: the server is on the left and the client on the right. After the connection is successful, the server sends "Welcome 127.0.0.1 to here!" to the customer !" Information, from the client
The server sends "hello"

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.