WinSocket Communication Test

Source: Internet
Author: User
Tags htons

First, the WinSocket uses the TCP protocol to complete the communication


Programs for ① servers

SocketSever.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <Winsock2.h> #include <stdio.h> #include <stdlib.h> #pragma comment (     LIB, "Ws2_32.lib") #if dubug #define AF_INET 2/* internetwork:udp, TCP, etc. */#define Sock_stream        1/* Stream socket */#define SOCK_DGRAM 2/* Datagram Socket */#define Sock_raw 3/* Raw-protocol interface */#define SOCK_RDM 4/* reliably-delivered message */#def INE Sock_seqpacket 5/* Sequenced Packet stream *//** Socket address, Internet style.*/struct sockaddr_in { Short Sin_family;u_short sin_port;struct in_addr sin_addr;char sin_zero[8];}; typedef struct SOCKADDR_IN sockaddr_in; /* Microsoft Windows Extended data Types */int bind (SOCKET s,const struct sockaddr FAR * name,int namelen);/* Incl_winsock _api_prototypes*/int Listen (SOCKET s,int backlog), #endif #define Port 3578#define MaxSize 1024sockaddr_in addrsrv; SockaDdr_in addrclient; SOCKET socksrv; Server Socket sockconn;//**************************************************************************************** * * Function Name * * STARTSOCK ()//** input * * No//** output * * No//** function Description * * Load socket//************************************ /int Socketinit () {WORD wversionrequested; Wsadata wsadata;int Err;wversionrequested=makeword (2,2); err = WSAStartup (wversionrequested,&wsadata); Returns 0, succeeds, otherwise the error code if (err!=0) {printf ("The WinSock DLL version is not required n"); return 0;} if (Lobyte (wsadata.wversion)!=2| | Hibyte (wsadata.wversion)!=2) {wsacleanup (); return 0;} return 1;}  //********************************************************************************************************///** Function Name * * Main ()//** input * * No//** output * * No//** function Description * * Main function//**************************************************************** /int Main () {if (Socket_error ==socketinit ()) {return-1;} Sockaddr_in addrsrv;//DepositAddrsrv.sin_addr to place local address information. S_un. S_addr=htonl (inaddr_any);//htol converts host byte-order long to network byte order addrsrv.sin_family=af_inet;addrsrv.sin_port=htons (6001);// Htos is used to convert the port into characters, more than 1024 of the number can//real socket Programming part socket Socksrv=socket (af_inet,sock_stream,0);//connection-oriented reliability service Sock_strambind (Socksrv, (sockaddr*) &addrsrv,sizeof (sockaddr));//Bind the socket to the corresponding address and port listen (socksrv,5);//The maximum length in the wait queue is 5printf ( "%s\n", "Welcome,the Host is running! Now wating for someone comes in! "); sockaddr_in addrclient;int isend = 0;char Sendbuf[maxsize];char revbuf[maxsize];int len=sizeof (SOCKADDR); while (1)// Loop listener client, never stop {sockconn=accept (socksrv, (sockaddr*) &addrclient,&len);//block the calling process until a new connection occurs if (Sockconn = = Invalid_socket) {printf ("Accept failed!\n"); return-1;} sprintf (SendBuf, "Server IP is:%s", Inet_ntoa (ADDRCLIENT.SIN_ADDR));//inet_nota function is converting characters to IP address isend = Send (Sockconn, Sendbuf,strlen (SENDBUF) +1,0);//The server sends data to the client if (isend = = socket_error) {printf ("Send failed!"); break;} Char recvbuf[100];recv (sockconn,recvbuf,100,0);//server accepts data from client printf ("%s\n", RECVBUF); clOsesocket (sockconn);//Close Socket}closesocket (sockconn); Close socket GetChar (); return 0;}
② Client-side programs

<pre name= "code" class= "CPP" > #include "stdafx.h" #include <Winsock2.h> #include <stdio.h> #include <string.h> #pragma comment (lib, "Ws2_32.lib")//************************************************************ * * Function Name * * STARTSOCK ()//** input * * No//** output * * No//** function Description * * Load socket//******** /int Socketinit () {WORD wversionrequested; Wsadata wsadata;int Err;wversionrequested=makeword (2,2); err = WSAStartup (wversionrequested,&wsadata); Returns 0, succeeds, otherwise the error code if (err!=0) {printf ("The WinSock DLL version is not required n"); return 0;} if (Lobyte (wsadata.wversion)!=2| | Hibyte (wsadata.wversion)!=2) {wsacleanup (); return 0;} return 1;}  //********************************************************************************************************///** Function Name * * Main ()//** input * * No//** output * * No//** function Description * * Main function//**************************************************************** ***************/int Main () {//fixed format char recvbuf[1024];if (Socket_error ==socketinit ()) {return-1;}    Establish communication socket Socket Sockclient=socket (af_inet,sock_stream,0);    Sockaddr_in addrsrv; Addrsrv.sin_addr. S_un.    S_ADDR=INET_ADDR ("192.168.1.100");//Set the IP address of the server to be connected addrsrv.sin_family=af_inet; Addrsrv.sin_port=htons (6001);//Set the port address of the server that needs to connect to connect (sockclient, (sockaddr*) &addrsrv,sizeof (sockaddr));//    Connect with the server//receive information from the teacher int irevlen = 0; Irevlen = recv (Sockclient,recvbuf,strlen (RECVBUF) +1,0); if (0 = = Irevlen) {return-1;} else if (socket_error = = Irevlen) {printf ("Recv failed\n!");    return-1;}    printf ("%s\n", recvbuf); char str[100];gets (str);    Send (Sockclient,str,strlen (str) +1,0); closesocket (sockclient); WSACleanup (); GetChar (); return-1;}


WinSocket Communication Test

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.