Simple TCPIP client server and tcpip Client Server

Source: Internet
Author: User

Simple TCPIP client server and tcpip Client Server

1 // soClient. cpp: Defines the entry point for the console application. 2 // 3 4 # include "stdafx. h "5 # include <winsock2.h> 6 # pragma comment (lib," ws2_32.lib ") 7 8 int _ tmain (int argc, _ TCHAR * argv []) 9 {10 WSADATA wsadata; 11 WORD dVer = MAKEWORD (2, 2); 12 WSAStartup (dVer, & wsadata); 13 14 socket s =: socket (AF_INET, SOCK_STREAM, IPPROTO_TCP ); 15 if (S = INVALID_SOCKET) 16 {17 return FALSE; 18} 19 sockaddr_in serverAddr; 20 serverAddr. sin_addr.S_un.S_addr = inet_addr ("127.0.0.1"); 21 serverAddr. sin_family = AF_INET; 22 serverAddr. sin_port = htons (4567); 23 if (: connect (S, (LPSOCKADDR) & serverAddr, sizeof (serverAddr) = SOCKET_ERROR) 24 {25 if (WSAGetLastError () = 10061) 26 {27 printf ("server not enabled"); 28} 29 return FALSE; 30} 31 32 char buff [256]; 33 int irecv = :: recv (S, buff, 256, 0); 34 if (irecv> 0) 35 {36 buff [irecv] = '\ 0 '; // The returned data will not end. Therefore, manually Add 37 printf ("returned data: % s", buff); 38} 39 40 closesocket (S); 41 return 0; 42}

 

 

 

 

Server

1 // soServer. cpp: Defines the entry point for the console application. 2 // 3 4 # include "stdafx. h "5 # include" winsock2.h "6 # pragma comment (lib," ws2_32.lib ") 7 8 int _ tmain (int argc, _ TCHAR * argv []) 9 {10 WSADATA wsadata; 11 WORD dVer = MAKEWORD (2, 2); 12 if (WSAStartup (dVer, & wsadata )! = 0) 13 {14 return FALSE; 15} 16 17 sockaddr_in sin; 18 sin. sin_family = AF_INET; 19 sin. sin_addr.S_un.S_addr = INADDR_ANY; 20 sin. sin_port = htons (4567); 21 socket s =: socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); 22 23 24 if (: bind (S, (LPSOCKADDR) & sin, sizeof (sin) = SOCKET_ERROR) 25 {26 return FALSE; 27} 28 29 if (: listen (S, 2) = SOCKET_ERROR) 30 {31 return FALSE; 32} 33 34 sockaddr_in remoteAddr; 35 int nAddrLen = sizeof (RemoteAddr); 36 SOCKET sClient; 37 char text [] = "you have connected! Welcome! "; 38 printf (" waiting to accept the connection. \ R \ n "); 39 while (TRUE) 40 {41 sClient =: accept (S, (LPSOCKADDR) & remoteAddr, & nAddrLen ); 42 if (sClient = SOCKET_ERROR) 43 {44 printf ("failed to get"); 45 continue; 46} 47 printf ("received new connection: % s ", inet_ntoa (remoteAddr. sin_addr); 48 send (sClient, text, strlen (text), 0); 49 closesocket (sClient); 50} 51 closesocket (S); 52 return 0; 53}

 

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.