client.c:
1 //Client.cpp 2 #include "StdAfx.h" 3 #include <winsock2.h> 4 #include <stdio.h> 5 #include <windows.h> 6 #pragma comment(lib,"ws2_32.lib") 7 int main(int argc, char* argv[]){ 8 //判断是否输入了IP地址和端口号 9 if(argc!=3){ 10 printf("Usage: %s IPAddress PortNumber/n",argv[0]); 11 exit(-1); 12 } 13 //把字符串的IP地址转化为u_long 14 unsigned long ip; 15 if((ip=inet_addr(argv[1]))==INADDR_NONE){ 16 printf("不合法的IP地址:%s",argv[1]); 17 exit(-1); 18 } 19 //把端口号转化成整数 20 short port; 21 if((port = atoi(argv[2]))==0){ 22 printf("端口号有误!"); 23 exit(-1); 24 } 25 printf("Connecting to %s:%d....../n",inet_ntoa(*(in_addr*)&ip),port); 26 WSADATA wsa; 27 //初始化套接字DLL 28 if(WSAStartup(MAKEWORD(2,2),&wsa)!=0){ 29 printf("套接字初始化失败!"); 30 exit(-1); 31 } 32 //创建套接字 33 SOCKET sock; 34 if((sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET){ 35 printf("创建套接字失败!"); 36 exit(-1); 37 } 38 struct sockaddr_in serverAddress; 39 memset(&serverAddress,0,sizeof(sockaddr_in)); 40 serverAddress.sin_family=AF_INET; 41 serverAddress.sin_addr.S_un.S_addr = ip; 42 serverAddress.sin_port = htons(port); 43 //建立和服务器的连接 44 if(connect(sock,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR){ 45 printf("建立连接失败!"); 46 exit(-1); 47 } 48 char buf[4096]; 49 // while(1){ 50 { 51 //printf(">"); 52 //从控制台读取一行数据 53 //gets(buf); 54 //发送给服务器 55 if(send(sock,"wangkangluo1",strlen(buf),0)==SOCKET_ERROR){ 56 printf("发送数据失败!"); 57 exit(-1); 58 } 59 int bytes; 60 if((bytes=recv(sock,buf,sizeof(buf),0))==SOCKET_ERROR){ 61 printf("接收数据失败!/n"); 62 exit(-1); 63 } 64 buf[bytes]='/0'; 65 printf("Message from %s: %s/n",inet_ntoa(serverAddress.sin_addr),buf); 66 } 67 //清理套接字占用的资源 68 WSACleanup(); 69 return 0; 70 }
server.c:
1 // reboot3.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 6 //Server.cpp 7 #include <winsock2.h> 8 #include <stdio.h> 9 #include <windows.h> 10 #pragma comment(lib,"ws2_32.lib") 11 int main(int argc, char* argv[]){ 12 //判断是否输入了端口号 13 if(argc!=2){ 14 printf("Usage: %s PortNumber/n",argv[0]); 15 exit(-1); 16 } 17 //把端口号转化成整数 18 short port; 19 if((port = atoi(argv[1]))==0){ 20 printf("端口号有误!"); 21 exit(-1); 22 } 23 WSADATA wsa; 24 //初始化套接字DLL 25 if(WSAStartup(MAKEWORD(2,2),&wsa)!=0){ 26 printf("套接字初始化失败!"); 27 exit(-1); 28 } 29 //创建套接字 30 SOCKET serverSocket; 31 if((serverSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET){ 32 printf("创建套接字失败!"); 33 exit(-1); 34 } 35 struct sockaddr_in serverAddress; 36 memset(&serverAddress,0,sizeof(sockaddr_in)); 37 serverAddress.sin_family=AF_INET; 38 serverAddress.sin_addr.S_un.S_addr = htonl(INADDR_ANY); 39 serverAddress.sin_port = htons(port); 40 //绑定 41 if(bind(serverSocket,(sockaddr*)&serverAddress,sizeof(serverAddress))==SOCKET_ERROR){ 42 printf("套接字绑定到端口失败!端口: %d/n",port); 43 exit(-1); 44 } 45 //进入侦听状态 46 if(listen(serverSocket,SOMAXCONN)==SOCKET_ERROR){ 47 printf("侦听失败!"); 48 exit(-1); 49 } 50 printf("Server %d is listening....../n",port); 51 SOCKET clientSocket;//用来和客户端通信的套接字 52 struct sockaddr_in clientAddress;//用来和客户端通信的套接字地址 53 memset(&clientAddress,0,sizeof(clientAddress)); 54 int addrlen = sizeof(clientAddress); 55 //接受连接 56 if((clientSocket=accept(serverSocket,(sockaddr*)&clientAddress,&addrlen))==INVALID_SOCKET){ 57 printf("接受客户端连接失败!"); 58 exit(-1); 59 } 60 printf("Accept connection from %s/n",inet_ntoa(clientAddress.sin_addr)); 61 char buf[4096]; 62 while(1){ 63 //接收数据 64 int bytes; 65 if((bytes=recv(clientSocket,buf,sizeof(buf),0))==SOCKET_ERROR){ 66 printf("接收数据失败!/n"); 67 exit(-1); 68 } 69 buf[bytes]='/0'; 70 printf("Message from %s: %s/n",inet_ntoa(clientAddress.sin_addr),buf); 71 if(send(clientSocket,buf,bytes,0)==SOCKET_ERROR){ 72 printf("发送数据失败!"); 73 exit(-1); 74 } 75 } 76 //清理套接字占用的资源 77 WSACleanup(); 78 return 0; 79 }
完
来源:https://www.cnblogs.com/wangkangluo1/archive/2012/05/01/2477966.html