一常用数据结构
1 SOCKET
typedef u_int SOCKET
其实就是一个无符号的整数,是一个socket描述符,我的理解就是 ,指向系统内核的”指针”,但这个”指针”要靠系统维护,我们只能获取,还要负责释放.
2 SOCKADDR_IN
struct sockaddr_in{
short sin_family;
unsigned short sin_port;
IN_ADDR sin_addr;
char sin_zero[8];
};
用来指定本地或远程的地址,以此来进行socket连接
sin_family:指定为AF_INET
sin_port: 指定所连接的端口号,用htons将short的端口号进行转换,如htons(6000)
sin_addr:指定所连接的ip,用inet_addr装换,如inet_addr(“127.0.0.1”)
sin_zero:为了和SOCKADDR的大小相同,而进行的填充,应该将其初始化为0
二 常用函数
1 WSAStartup
原型:
int WSAStartup(WORD wVersionRequested,LPWSADATA lpWSAData)
作用:
用来初始化ws2_32.lib
返回值:
成功返回0
2 WSACleanup
终止ws2_32.lib
3 socket
原型:
SOCKET socket (int af, int type, int protocol );
参数说明
af:指定地址家族,AF_INET
type: SOCK_STREAM或SOCK_DGRAM,前者用于TCP,后者用于UDP
protocol:设置为0即可
作用:
创建一个SOCKET
返回值
创建失败返回INVALID_SOCKET
4 bind
原型:
int bind( SOCKET s, const struct sockaddr FAR *name,int namelen );
参数说明
s:server端已经创建好的SOCKET
name:server的SOCKADDR_IN结构,需要做强制类型转换,注意,他和SOCKADDR的大小一样
namelen:顾名思义,name的size大小,注意他和SOCKADDR的大小一样
作用
将SOCKET绑定到机器上
返回值
成功返回0,错误返回SOCKET_ERROR
5 listen
原型
int listen( SOCKET s, int backlog );
参数说明
s: 服务端未连接的SOCKET
backlog:最大可连接数
作用
设置tcp服务端
返回值
成功返回0,失败返回SOCKET_ERROR
6 accept
原型
SOCKET accept( SOCKET s, struct sockaddr FAR *addr, int FAR *addrlen);
参数说明
s:tcp服务端的SOCKET
addr:远程客户端的地址信息
addlen:addr的size
作用
从已经获得许可的连接中关联一个新的SOCKET
返回值
成功返回新的SOCKET的值,错误返回INVALID_SOCKET
7 send
原型
int send(SOCKET s, const char FAR *buf, int len, int flags );
参数说明
s:对方的SOCKET
buf:要发送的缓冲区的地址
flags:通常置为0
作用
向对方发送数据
返回值
成功返回已发送的数据的字节数,错误返回SOCKET_ERROR
8 recv
原型
int recv( SOCKET s, char FAR *buf, int len, int flags );
参数说明
s:对方的SOCKET
buf:要发送的缓冲区的地址
flags:通常置为0
作用
接受对方的数据
返回值
成功返回已接受的数据的字节数,错误返回SOCKET_ERROR
9 closesocket
原型
int closesocket( SOCKET s );
参数说明
s:要关闭的SOCKET
作用
关闭指定的SOCKET
返回值
成功返回0,错误返回SOCKET_ERROR
10 connect
原型
Int connect(SOCKET s,const struct sockaddr FAR *name,int namelen) );
参数说明
S:本地SOCKET
name:远程服务端地址信息
namelen:name的size
作用
远程socket连接
返回值
成功返回0,错误返回SOCKET_ERROR
11 sendto
原型
int sendto( SOCKET s, const char FAR *buf, int len, int flags, const struct sockaddr FAR *to, int tolen); );
参数说明
S:对方的SOCKET,可能是连接状态
buf:要发送的信息地址
len:信息的大小
flags:通常置为0
to:远程地址信息
tolen:to的size
作用
用于UDP,数据的发送
返回值
成功返回已发送的数据的字节数,错误返回SOCKET_ERROR
12 recvfrom
原型
int recvfrom( SOCKET s, char FAR* buf, int len, int flags, struct sockaddr FAR *from, int FAR *fromlen );
参数说明
S:对方的SOCKET,可能是连接状态
buf:用于接收的缓冲区地址
len:信息的大小
flags:通常置为0
from:接收远程地址信息
fromlen:to的size
作用
用于UDP,数据的接收
返回值
成功返回已接收的数据的字节数,错误返回SOCKET_ERROR
15 WSAGetLastError
原型: int WSAGetLastError (void);
作用:返回最后一次的错误代码
错误代码在WINSOCK2.H中定义
14 一些转换函数
ntohs:”network to host short”
ntohl:”network to host long”
htons:”host to network short”
htonl:”host to network long”
inet_addr:将字符串的IP转换为网络IP
三正式编程
以下程序在vc6.0下编译
1 tcp编程
TCP Server:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #include <WINSOCK2.H>
- #include <windows.h>
- #include <iostream>
- #include <string>
- using namespace std;
- #pragma comment(lib, "ws2_32.lib ")
-
-
-
- bool LoadWSA();
-
- bool CreateSocket(SOCKET& sock,int type=SOCK_STREAM);
-
- bool BindSocket(SOCKET& sock,short port ,const char* strIp);
-
- bool SetListen(SOCKET& sock,int number);
-
- bool Communicate(SOCKET& srverSock);
-
- bool CloseSocket(SOCKET& sock);
-
- void CloseWSA();
-
- void ExitWithErrorMessage(const string& errMsg);
-
- DWORD WINAPI ClientThread(LPVOID sockCliet);
-
- void main()
- {
- cout<<"start server.\n";
-
- if(!LoadWSA() )
- ExitWithErrorMessage("LoadWSA failed.");
- cout<<"LoadWAS success.\n";
-
- SOCKET serSock;
-
- if(!CreateSocket(serSock) )
- ExitWithErrorMessage("create server socket failed.");
- cout<<"CreateSocket success.\n";
-
- if(!BindSocket(serSock,6000,"127.0.0.1") )
- ExitWithErrorMessage("BindSocket failed.");
- cout<<"BindSocket success.\n";
-
- if(!SetListen(serSock,10) )
- ExitWithErrorMessage("SetListen failed.");
- cout<<"SetListen success.\n";
-
- if(!Communicate(serSock) )
- ExitWithErrorMessage("Communicate failed.");
- cout<<"Communicate success.\n";
-
- if(! CloseSocket(serSock) )
- ExitWithErrorMessage("CloseSocket failed.");
- cout<<"CloseSocket success.\n";
-
- CloseWSA();
- cout<<"CloseWSA success.\n";
- }
-
-
- bool LoadWSA()
- {
- WSAData wsaData;
-
- if( 0 == WSAStartup(MAKEWORD(2,2),&wsaData) )
- {
- return true;
- }
-
- return false;
- }
-
- void CloseWSA()
- {
- WSACleanup();
- }
-
- bool CreateSocket(SOCKET& sock,int type)
- {
- sock=socket(AF_INET,type,0);
-
- if(INVALID_SOCKET == sock)
- {
- return false;
- }
-
- return true;
- }
-
- bool BindSocket(SOCKET& sock,short port ,const char* strIp)
- {
- SOCKADDR_IN address;
-
- address.sin_family=AF_INET;
- address.sin_addr.s_addr =inet_addr(strIp);
- address.sin_port=htons(port);
- memset(address.sin_zero,0,sizeof(address.sin_zero));
-
- if(SOCKET_ERROR == bind(sock,(const struct sockaddr*)&address,sizeof(struct sockaddr)) )
- {
- return false;
- }
-
- return true;
- }
-
- bool SetListen(SOCKET& sock,int number)
- {
- if(SOCKET_ERROR == listen(sock,number) )
- {
- return false;
- }
-
- return true;
- }
-
- bool Communicate(SOCKET& srverSock)
- {
- SOCKADDR_IN addrClient;
- SOCKET newSock;
- HANDLE hThread=NULL;
- int n=sizeof(struct sockaddr);
- bool flag=true;
-
- while(true)
- {
- newSock=accept(srverSock,( struct sockaddr*)&addrClient,&n);
-
- if(newSock == INVALID_SOCKET)
- {
- cout<<"accept failed.\n";
- flag=false;
- break;
- }
-
- cout<<"client from "<<inet_ntoa(addrClient.sin_addr)<<":"<<ntohs(addrClient.sin_port)<<endl;
-
- hThread=CreateThread(NULL,0,ClientThread,(LPVOID)newSock,0,NULL);
-
- if(hThread==NULL)
- {
- cout<<"CreateThread failed.\n";
- flag=false;
- break;
- }
-
- CloseHandle(hThread);
- }
-
- closesocket(newSock);
-
- return flag;
- }
-
-
- bool CloseSocket(SOCKET& sock)
- {
- if( closesocket(sock) == SOCKET_ERROR)
- return false;
- return true;
- }
-
- void ExitWithErrorMessage(const string& errMsg)
- {
- cout<<errMsg<<endl;
- system("pause");
- exit(1);
- }
-
- DWORD WINAPI ClientThread(LPVOID sockCliet)
- {
- SOCKET sock=(SOCKET)sockCliet;
- char buffer[100]={0};
- int errCode;
-
- while(true)
- {
- memset(buffer,0,100);
- errCode=recv(sock,buffer,100,0);
-
- if(errCode==0 || errCode==SOCKET_ERROR)
- {
- cout<<"client exit.\n";
- break;
- }
-
- cout<<"message from client: "<<buffer<<endl;
-
- send(sock,"welcome.",strlen("welcome."),0);
- }
-
- return 0;
- }
TCP Client
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #include <WINSOCK2.H>
- #include <windows.h>
- #include <iostream>
- #include <string>
- using namespace std;
- #pragma comment(lib, "ws2_32.lib ")
-
-
-
- bool LoadWSA();
-
- bool CreateSocket(SOCKET& sock,int type=SOCK_STREAM);
-
- bool ConnectToServer(SOCKET& sock,short port ,const char* strIp);
-
- bool Communicate(SOCKET& sock);
-
- bool CloseSocket(SOCKET& sock);
-
- void CloseWSA();
-
- void ExitWithErrorMessage(const string& errMsg);
-
-
- void main()
- {
- cout<<"Start Client.\n";
- if(!LoadWSA() )
- ExitWithErrorMessage("LoadWSA failed.");
- cout<<"LoadWSA Success.\n";
-
- SOCKET clietSock;
-
- if(!CreateSocket(clietSock) )
- ExitWithErrorMessage("create server socket failed.");
- cout<<"CreateSocket Success.\n";
-
- if(!ConnectToServer(clietSock,6000,"127.0.0.1") )
- ExitWithErrorMessage("ConnectToServer failed.");
- cout<<"ConnectToServer Success.\n";
-
- if(!Communicate(clietSock) )
- ExitWithErrorMessage("Communicate failed.");
- cout<<"Communicate Success.\n";
-
- if(! CloseSocket(clietSock) )
- ExitWithErrorMessage("CloseSocket failed.");
- cout<<"CloseSocket Success.\n";
-
- CloseWSA();
- cout<<"CloseWSA Success.\n";
-
-
- }
-
- bool LoadWSA()
- {
- WSAData wsaData;
-
- if( 0 == WSAStartup(MAKEWORD(2,2),&wsaData) )
- {
- return true;
- }
-
- return false;
- }
-
- void CloseWSA()
- {
- WSACleanup();
- }
-
- bool CreateSocket(SOCKET& sock,int type)
- {
- sock=socket(AF_INET,type,0);
-
- if(INVALID_SOCKET == sock)
- {
- return false;
- }
-
- return true;
- }
-
- bool ConnectToServer(SOCKET& sock,short port ,const char* strIp)
- {
- SOCKADDR_IN address;
-
- address.sin_family=AF_INET;
- address.sin_addr.s_addr=inet_addr(strIp);
- address.sin_port=htons(port);
- memset(address.sin_zero,0,sizeof(address.sin_zero));
-
- if(SOCKET_ERROR == connect(sock,(const struct sockaddr*)&address,sizeof(struct sockaddr)) )
- {
- return false;
- }
-
- return true;
- }
-
- bool Communicate(SOCKET& sock)
- {
- bool flag=true;
- int errCode;
- char buffer[100]={0};
-
- while(true)
- {
- memset(buffer,0,100);
- cin.getline(buffer,100);
- errCode=send(sock,buffer,strlen(buffer),0);
-
- if(errCode==SOCKET_ERROR)
- {
- cout<<"send message error.\n";
- flag=false;
- break;
- }
-
- memset(buffer,0,100);
- recv(sock,buffer,100,0);
- cout<<buffer<<endl;
- }
-
- return flag;
- }
-
-
-
- void ExitWithErrorMessage(const string& errMsg)
- {
- cout<<errMsg<<endl;
- system("pause");
- exit(1);
- }
-
- bool CloseSocket(SOCKET& sock)
- {
- if( closesocket(sock)==SOCKET_ERROR )
- return false;
-
- return true;
- }
2 upd编程
UDP Server
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #include <WINSOCK2.H>
- #include <windows.h>
- #include <iostream>
- #include <string>
- using namespace std;
- #pragma comment(lib, "ws2_32.lib ")
-
-
-
- bool LoadWSA();
-
- bool CreateSocket(SOCKET& sock,int type=SOCK_DGRAM);
-
- bool BindSocket(SOCKET& sock,short port ,const char* strIp);
-
- bool Communicate(SOCKET& srverSock);
-
- bool CloseSocket(SOCKET& sock);
-
- void CloseWSA();
-
- void ExitWithErrorMessage(const string& errMsg);
-
-
- void main()
- {
- if(! LoadWSA() )
- ExitWithErrorMessage("LoadWSA falid.");
- cout<<"LoadWSA success.\n";
-
- SOCKET serSocket;
- if(! CreateSocket(serSocket) )
- ExitWithErrorMessage("CreateSocket falid.");
- cout<<"CreateSocket success.\n";
-
- if(! BindSocket(serSocket,6000,"127.0.0.1") )
- ExitWithErrorMessage("BindSocket falid.");
- cout<<"BindSocket success.\n";
-
- while(! Communicate(serSocket) );
- cout<<"Communicate success.\n";
-
- if(! CloseSocket(serSocket) )
- ExitWithErrorMessage("CloseSocket falid.");
- cout<<"CloseSocket success.\n";
-
- CloseWSA();
- }
-
-
- bool LoadWSA()
- {
- WSAData wsaData;
-
- if( 0 == WSAStartup(MAKEWORD(2,2),&wsaData) )
- {
- return true;
- }
-
- return false;
- }
-
- void CloseWSA()
- {
- WSACleanup();
- }
-
- bool CreateSocket(SOCKET& sock,int type)
- {
- sock=socket(AF_INET,type,0);
-
- if(INVALID_SOCKET == sock)
- {
- return false;
- }
-
- return true;
- }
-
- bool BindSocket(SOCKET& sock,short port ,const char* strIp)
- {
- SOCKADDR_IN address;
-
- address.sin_family=AF_INET;
- address.sin_addr.s_addr =inet_addr(strIp);
- address.sin_port=htons(port);
- memset(address.sin_zero,0,sizeof(address.sin_zero));
-
- if(SOCKET_ERROR == bind(sock,(const struct sockaddr*)&address,sizeof(struct sockaddr)) )
- {
- return false;
- }
-
- return true;
- }
-
- bool Communicate(SOCKET& srverSock)
- {
- bool flag =true;
- SOCKADDR_IN address;
- int n=sizeof(struct sockaddr);
- char buffer[100] = {0};
-
- if(recvfrom(srverSock,buffer, 100,0,(struct sockaddr*)&address,&n) ==SOCKET_ERROR )
- flag =false;
-
- if(flag)
- cout<<buffer;
-
- return flag;
- }
-
- void ExitWithErrorMessage(const string& errMsg)
- {
- cout<<errMsg<<endl;
- system("pause");
- exit(1);
- }
-
- bool CloseSocket(SOCKET& sock)
- {
- if( closesocket(sock)==SOCKET_ERROR )
- return false;
-
- return true;
- }
TCP Client
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #include <WINSOCK2.H>
- #include <windows.h>
- #include <iostream>
- #include <string>
- using namespace std;
- #pragma comment(lib, "ws2_32.lib ")
-
-
-
- bool LoadWSA();
-
- bool CreateSocket(SOCKET& sock,int type=SOCK_DGRAM);
-
- bool Communicate(SOCKET& clientSock,short port ,const char* strIp);
-
- bool CloseSocket(SOCKET& sock);
-
- void CloseWSA();
-
- void ExitWithErrorMessage(const string& errMsg);
-
-
- void main()
- {
- if(! LoadWSA() )
- ExitWithErrorMessage("LoadWSA falid.");
- cout<<"LoadWSA success.\n";
-
- SOCKET clientSocket;
- if(! CreateSocket( clientSocket) )
- ExitWithErrorMessage("CreateSocket falid.");
- cout<<"CreateSocket success.\n";
-
- if(! Communicate( clientSocket,6000,"127.0.0.1") )
- ExitWithErrorMessage("Communicate falid.");
- cout<<"Communicate success.\n";
-
- if(! CloseSocket( clientSocket) )
- ExitWithErrorMessage("CloseSocket falid.");
- cout<<"CloseSocket success.\n";
-
- CloseWSA();
- }
-
-
- bool LoadWSA()
- {
- WSAData wsaData;
-
- if( 0 == WSAStartup(MAKEWORD(2,2),&wsaData) )
- {
- return true;
- }
-
- return false;
- }
-
- void CloseWSA()
- {
- WSACleanup();
- }
-
- bool CreateSocket(SOCKET& sock,int type)
- {
- sock=socket(AF_INET,type,0);
-
- if(INVALID_SOCKET == sock)
- {
- return false;
- }
-
- return true;
- }
-
-
- bool Communicate(SOCKET& clientSock,short port ,const char* strIp)
- {
- bool flag =true;
- SOCKADDR_IN address;
- int n=sizeof(struct sockaddr);
-
- address.sin_family=AF_INET;
- address.sin_addr.s_addr=inet_addr(strIp);
- address.sin_port=htons(port);
- memset(address.sin_zero,0,sizeof(address.sin_zero));
-
- if( sendto(clientSock,"hello , I am from client message.", sizeof("hello , I am from server message."),
- 0,(const struct sockaddr*)&address,sizeof(struct sockaddr) ) ==SOCKET_ERROR )
- flag =false;
-
- return flag;
- }
-
- void ExitWithErrorMessage(const string& errMsg)
- {
- cout<<errMsg<<endl;
- system("pause");
- exit(1);
- }
-
- bool CloseSocket(SOCKET& sock)
- {
- if( closesocket(sock)==SOCKET_ERROR )
- return false;
-
- return true;
- }