#include <Winsock2.h> #include <iostream> #include <string> #pragma comment(lib,"ws2_32.lib") using namespace std; void main() { //加载套接字库 WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData );//该函数的功能是加载一个Winsocket库版本 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 = inet_addr("192.168.1.14"); addrSrv.sin_family = AF_INET; addrSrv.sin_port = htons(8080); //将套接字绑定到端口上 bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); //将套接字设置为监听模式 listen(sockSrv,5); //等待客户请求来到,当请求来到时候,接受请求,接受连接请求,返回一个新的对应于此连接套接字 SOCKADDR_IN addrClient; int len = sizeof(SOCKADDR); //开始监听 cout<<"等待用户连接"<<endl; SOCKET sockConn = accept(sockSrv,(SOCKADDR*)&addrClient,&len);//sockConn用于建立连接的套接字 cout<<"用户连接到来"<<endl; //和客户通信 //接收数据 char recvBuffer[300];//接收字符数据 memset((void*)recvBuffer,'\0',300); cout<<"等待对方发送数据... "<<endl; recv(sockConn,recvBuffer,100,0); cout<<"对方的地址为: "<<inet_ntoa(addrClient.sin_addr)<<endl; cout<<"接收的内容为: "<<recvBuffer<<endl; //发送数据 string sendBuffer = "this is server"; cout<<"向客户端方发送数据: "<<sendBuffer.c_str()<<endl; send(sockConn,sendBuffer.c_str(),sendBuffer.size(),0); //关闭本次连接的通道 closesocket(sockConn); closesocket(sockSrv);//关闭服务器套接字 WSACleanup();//结束套接字库的调用 system("pause"); }
服务端:
#include <Winsock2.h> #include <iostream> #include <string> #pragma comment(lib,"ws2_32.lib") using namespace std; void main() { //加载套接字库 WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData );//该函数的功能是加载一个Winsocket库版本 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 = inet_addr("192.168.1.14"); addrSrv.sin_family = AF_INET; addrSrv.sin_port = htons(8080); //将套接字绑定到端口上 bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR)); //将套接字设置为监听模式 listen(sockSrv,5); //等待客户请求来到,当请求来到时候,接受请求,接受连接请求,返回一个新的对应于此连接套接字 SOCKADDR_IN addrClient; int len = sizeof(SOCKADDR); //开始监听 cout<<"等待用户连接"<<endl; SOCKET sersock = accept(sockSrv,(SOCKADDR*)&addrClient,&len);//sockConn用于建立连接的套接字 cout<<"用户连接到来"<<endl; ///////////////////////////////////////////////////////////////////////////////////////////////////////// //和客户通信 //接收数据 int choice=0; int i=0; char temp_L=0; char temp_H=0; char CIN_Data; int CIN_Data_len; int CIN_Data_index=0; char msg[10000]; memset(msg,0,sizeof(msg)); cout<<"请输入功能 1 发送 2 接受 0 退出 "<<endl; while (cin>>choice&&choice!=0) { CIN_Data_index=0; memset(msg,0,sizeof(msg)); if (choice==1) { cout<<"选择的是发送功能:"<<endl; cout<<" 请输入数据长度: "; cin>>CIN_Data_len; cout<<"请输入"<<CIN_Data_len<<"个十六进制数据"; while (CIN_Data_len>CIN_Data_index) { scanf("%x",&CIN_Data); msg[CIN_Data_index]=CIN_Data; CIN_Data_index++; } send(sersock,msg,strlen(msg),0 ); //发出输入信息 cout<<"请输入功能 1 发送 2 接受 0 退出 "<<endl; } if (choice==2) { cout<<"等待数据..."<<endl; len = recv ( sersock,msg,200,0 ); //接收用户信息 printf("\n收到信息:%s",msg); for ( i=0;i<len;i++) { cout<<"msg[]"<<i<<endl; temp_L=msg[i]&0x0f; temp_H=msg[i]>>4; temp_H=temp_H&0x0f; cout<<"temp_L & temp_H :"<<(int)temp_H<<" "<<(int)temp_L<<endl; } cout<<"请输入功能 1 发送 2 接受 0 退出 "<<endl;; } } /* char recvBuffer[300];//接收字符数据 memset((void*)recvBuffer,'\0',300); cout<<"等待对方发送数据... "<<endl; recv(sockConn,recvBuffer,100,0); cout<<"对方的地址为: "<<inet_ntoa(addrClient.sin_addr)<<endl; cout<<"接收的内容为: "<<recvBuffer<<endl; //发送数据 string sendBuffer = "this is server"; cout<<"向客户端方发送数据: "<<sendBuffer.c_str()<<endl; send(sockConn,sendBuffer.c_str(),sendBuffer.size(),0); */ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //关闭本次连接的通道 closesocket(sersock); closesocket(sockSrv);//关闭服务器套接字 WSACleanup();//结束套接字库的调用 system("pause"); }
#include <cstdlib> #include <iostream> #include <conio.h> #include <stdio.h> #include <string.h> #include <winsock2.h> //Windows Sockets函数定义 #include <stdlib.h> using namespace std; bool InitSocket(); //初始化Windows Sockets动态连接库 #pragma comment(lib, "Ws2_32.lib") int main(int argc, char *argv[]) { int type; SOCKET sock; //套接字 struct sockaddr addr; //地址 int len,portno; int err; char msg[255]; bool over; int i; char temp_H=0; char temp_L=0; if ( !InitSocket() ) return 0; //初始化Window Sockets DLL type = SOCK_STREAM; //面向连接的流式Socket类型 sock = socket( AF_INET,type,0 ); //创建支持Internet协议的流式Socket if ( sock==INVALID_SOCKET ) //不能创建,返回 { printf("不能创建Socket!"); getch(); WSACleanup( ); //卸载Window Sockets DLL return 0; } //客户端地址设定 ((sockaddr_in*)&addr)->sin_family = AF_INET; //AF_INET:使用Internet 协议 ((sockaddr_in*)&addr)->sin_port = 0; //客户端端口号:0--系统分配 ((sockaddr_in*)&addr)->sin_addr.s_addr = 0; //主机地址:0--本机 bind(sock,&addr,sizeof(addr)); //把地址与Socket绑定 //输入服务器地址 ((sockaddr_in*)&addr)->sin_family = AF_INET; //AF_INET:使用Internet 协议 printf("输入服务器地址(0.0.0.0):"); gets(msg); if (msg[0]=='\0') sprintf(msg,"0.0.0.0"); //如果直接回车,就采用默认设置(0.0.0.0):本机 ((sockaddr_in*)&addr)->sin_addr.s_addr = inet_addr(msg); //服务器地址 printf("输入服务器端口号(3000):"); gets(msg); if (msg[0]=='\0') portno=3000; //如果直接回车,就采用默认端口号3000 else portno=atoi(msg); ((sockaddr_in*)&addr)->sin_port = htons( portno ); //服务器端口号; //htons():把主机表示的短整数转化成网络字节顺序 len = sizeof(addr); printf("与服务器连接...!"); err = connect( sock,(sockaddr*)&addr,len ); //在使用面向连接协议时(TCP),必须与服务器连接成功后,才可通信 //在无连接协议(UDP)中,可以直接向对方发数据,而无需连接 if ( err==SOCKET_ERROR ) { printf("连接失败!"); getch(); WSACleanup( ); //卸载Window Sockets DLL return 0; } printf("成功连接到服务器!"); cout<<endl; int choice=0; char CIN_Data; int CIN_Data_len; int CIN_Data_index=0; cout<<"请输入功能 1 发送 2 接受 0 退出 "<<endl; while (cin>>choice&&choice!=0) { CIN_Data_index=0; memset(msg,0,sizeof(msg)); if (choice==1) { cout<<"选择的是发送功能:"<<endl; cout<<" 请输入数据长度: "; cin>>CIN_Data_len; cout<<"请输入"<<CIN_Data_len<<"个十六进制数据"; while (CIN_Data_len>CIN_Data_index) { scanf("%x",&CIN_Data); msg[CIN_Data_index]=CIN_Data; CIN_Data_index++; } send( sock,msg,strlen(msg),0 ); //发送输入信息 cout<<"请输入功能 1 发送 2 接受 0 退出 "<<endl; } if (choice==2) { cout<<"等待数据..."<<endl; len = recv ( sock,msg,200,0 ); printf("\n收到信息:"); printf(msg); //显示接受信息 cout<<endl; for ( i=0;i<len;i++) { cout<<"msg[]"<<i<<endl; temp_L=msg[i]&0x0f; temp_H=msg[i]>>4; temp_H=temp_H&0x0f; cout<<"temp_L & temp_H :"<<(int)temp_H<<" "<<(int)temp_L<<endl; } cout<<"请输入功能 1 发送 2 接受 0 退出 "<<endl; } } closesocket(sock); WSACleanup( ); //卸载Window Sockets DLL return 0; } bool InitSocket() { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 2, 0 ); //Winsock2.0 //初始化Windows Sockets DLL, err = WSAStartup( wVersionRequested, &wsaData ); //加载Winsock DLL到内存 if ( err != 0 ) { printf("没有Windows Socket动态库!\n"); getch(); return false; } if ( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 0 ) { printf("需要Windows Socket 2!\n"); getch(); WSACleanup( ); //非Winsock2.0时,卸载Window Sockets DLL return false; } return true; }