先举个例子来说明下套接字
地区a的区号为123,b地区的区号为321,a地区的电话号码66219677要给b地区的电话号码55768444打电话。
对应为
Ip为192.168.1.1 端口号为2000的进程 要和Ip为 192.168.1.2 端口号为3000的进程通信。
不知道这样比喻行不?
一般啊,同一个IP下的进程端口号不会重复的,就如你的电话号码一样。
socket (套接字),用于描述IP地址和端口,是一个通信链的句柄,当然描述还有协议,应用程序通常通过"套接字"向网络发出请求或者应答网络请求。
我们打电话之前,双方必须各自拥有一台电话机,同样,进行socket通信,双方需要有各自的socket,socket实质上提供了进程通信的端点。
服务器端:
#include <Winsock2.h>
#include <stdio.h>
#pragma comment(lib,"WS2_32.LIB")
#define Port 3000
#define MaxSize 1024
void main()
{
<wbr><wbr><wbr>//加载套接字<br><wbr><wbr><wbr> WORDwVersionRequested;<br><wbr><wbr><wbr> WSADATAwsaData;<br><wbr><wbr><wbr> int err;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr>wVersionRequested=MAKEWORD(2,2);</wbr></wbr></wbr>
<wbr><wbr><wbr> err =WSAStartup(wVersionRequested,&wsaData);<br><wbr><wbr><wbr> if(err!=0)<br><wbr><wbr><wbr> {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>printf("WinSock DLL版本有问题");<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return;<br><wbr><wbr><wbr> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr> if(LOBYTE(wsaData.wVersion)!=2||<br><wbr><wbr><wbr>HIBYTE(wsaData.wVersion)!=2)<br><wbr><wbr><wbr> {<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>WSACleanup();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return;<br><wbr><wbr><wbr><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>//创建监听的套接字<br><wbr><wbr><wbr><wbr>SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>SOCKADDR_IN addrSrv;</wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr>//把U_LONG的主机字节顺序转换为TCP/IP网络字节顺序<br><wbr><wbr><wbr><wbr>addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>addrSrv.sin_family = AF_INET;<br><wbr><wbr><wbr><wbr>addrSrv.sin_port = htons(Port);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>//绑定套接字<br><wbr><wbr><wbr><wbr>bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));<br><wbr><wbr><wbr><wbr>//将套接字设置为监听模式,准备接受用户请求<br><wbr><wbr><wbr><wbr><wbr>listen(sockSrv,5);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr>SOCKADDR_INaddrClient;<br><wbr><wbr><wbr><wbr>int len=sizeof(SOCKADDR);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr>printf("%sn","Welcome,the Host is running!nWating for someone comesin!");<br><wbr><wbr><wbr><wbr>while (1)<br><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//等待用户请求到来<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>SOCKETsockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>charsendBuf[MaxSize];<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>sprintf(sendBuf,"Welcome to the Host,%s.",inet_ntoa(addrClient.sin_addr));<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//发送数据<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>send(sockConn,sendBuf,MaxSize,0);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//接收数据<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>charrevBuf[MaxSize];<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>char name[20] = {0};<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int flag = 1;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>while(recv(sockConn,revBuf,MaxSize,0) != -1)<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//打印接受数据<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>if(flag == 1)<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>strcpy(name, revBuf);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>printf("%s comes in!n",name);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>flag = 2;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>