C ++ Socket programming steps and socket programming steps

Source: Internet
Author: User

C ++ Socket programming steps and socket programming steps

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.

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 ()).

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 ()).

Method 1: load/release the Winsock database:

1. Loading Method:

WSADATA wsa;
/* Initialize socket Resources */
If (WSAStartup (MAKEWORD (1,1), & wsa )! = 0)
{
Return; // indicates an error.
}

2. Release Method:

WSACleanup();

Method 2: Construct a SOCKET:

1. SERVER: Construct a listener SOCKET and a streaming SOCKET.
SOCKETListen_Sock= Socket (AF_INET, SOCK_STREAM, 0)

2. Client: Construct communication SOCKET and stream SOCKET.
SOCKET Client_ Sock= Socket (AF_INET, SOCK_STREAM, 0)

Method 3: configure the listening address and port:

1. SERVER: SOCKADDR_INServerAddr
ZeroMemory (char *)&ServerAddr, Sizeof (ServerAddr));
ServerAddr. Sin_family =AF_INET;
ServerAddr. Sin_port = htons (1234);/* Local port: 1234 */
ServerAddr. Sin_addr.s_addr = htonl (INADDR_ANY);/* With IP */

Method 4: bind a SOCKET:

1. SERVER: bind the listening SOCKET.
Bind (Listen_Sock,(Struct sockaddr *)&ServerAddr,Sizeof (ServerAddr))

Method 5: Server/client connection:

1. SERVER: Waiting for client access.
SOCKETCommand_Sock= Accept (Listen_Sock,...)

2. Client: request to connect to the server.
Int ret = connect (Client_Sock,...)

Method 6: receive/send data:

1. SERVER: Wait for the client to access. char buf [1024].
Receive data: recv (Command_Sock, Buf ,...)
Or
Send data: send (Command_Sock, Buf ,...)

2. Client: request to connect to the server. char buf [1024].
Send data: send (Client_ Sock, Buf ,...)
Or
Receive data: recv (Client_ Sock, Buf ,...)

Method 7: Disable SOCKET:

1. SERVER: Disable SOCKET.
Closesocket (Listen_Sock)
Closesocket (Command_Sock)

2. Client: Disable SOCKET.
Closesocket (Client_Sock)

#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 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); } }
#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();
}

 

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.