C + + Socket programming steps

Source: Internet
Author: User

Sockets (sockets) are programmed with three types of streaming sockets (SOCK_STREAM), datagram Sockets (SOCK_DGRAM), raw Sockets (Sock_raw), and TCP-based socket programming as a streaming socket.

Steps for server-side programming:

1: Load socket font, create socket (WSAStartup ()/socket ());

2: Bind sockets to an IP address and a port (bind ());

3: Set socket to listening mode waiting for connection request (listen ());

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

5: Communicates with the client using the returned socket (send ()/recv ());

6: Return, wait for another connection request;

7: Close the socket and close the loaded socket font (closesocket ()/wsacleanup ()).

Client-side programming steps:

1: Load socket font, create socket (WSAStartup ()/socket ());

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

3: Communicate with the server (send ()/recv ());

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

First: Load/release Winsock library:

1. Loading method:

Wsadata WSA;
/* Initialize the socket resource */
if (WSAStartup (Makeword), &WSA)! = 0)
{
Return Representative failure
}

2. Release method:

WSACleanup ();

Second type: Construct socket:

1. Server: Construct the monitor socket, stream socket.
Socket Listen_sock = socket (af_inet, sock_stream, 0)

2. Client: Constructs communication socket, streaming socket.
Socket Client_sock = socket (af_inet, sock_stream, 0)

The third type: Configure the listening address and port:

1. Service side: sockaddr_in serveraddr
ZeroMemory (char *) &serveraddr, sizeof (serveraddr));
serveraddr. sin_family = af_inet;
serveraddr. Sin_port = htons (1234); /* Local listening port: 1234*/
serveraddr. sin_addr.s_addr = htonl (inaddr_any); /* with ip*/

Type IV: BIND socket:

1. Server: Bind listening socket.
bind (listen_sock,(struct sockaddr *) &serveraddr,sizeof (serveraddr))

Type V: Server/Client Connection:

1. Server: Waiting for client access.
SOCKET Command_sock = Accept (listen_sock, ...)

2. Client: The request is connected to the server.
int ret = connect (client_sock, ...)

Type VI: Collect/Send data:

1. Server: Wait for client access. Char buf[1024].
Data received: recv (command_sock, buf, ...)
Or
Send data: Send (command_sock, buf, ...)

2. Client: The request is connected to the server. Char buf[1024].
Send data: Send (Client_sock, buf, ...)
Or
Receive data: recv (Client_sock, buf, ...)

Type seventh: Close socket:

1. Server: Close socket.
Closesocket (listen_sock)
Closesocket (command_sock)

2. Client: Close the socket.
Closesocket (client_sock)

#include <stdio.h>#include <Winsock2.h>voidmain(){ WORD wVersionRequested; WSADATA wsaData; interr;  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; intlen=sizeof(SOCKADDR); while(1) {  SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);  charsendBuf[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); }}
#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));
Send (sockclient, "Hello", strlen ("Hello") +1,0);
Char recvbuf[50];
Recv (sockclient,recvbuf,50,0);
printf ("%s\n", recvbuf);

Closesocket (sockclient);
WSACleanup ();

C + + Socket programming steps

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.