1、Socket服务器端:
Socket服务器端流程如下:加载套接字->创建监听的套接字->绑定套接字->监听套接字->处理客户端相关请求。
下面是孙鑫VC详解里面的服务器端的例子:
- #include <Winsock2.h>
- #include <stdio.h>
-
- void main()
- {
-
- WORD wVersionRequested;
- WSADATA wsaData;
- int err;
-
- wVersionRequested=MAKEWORD(1,1);
-
- err=WSAStartup(wVersionRequested,&wsaData);
- 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=htonl(INADDR_ANY);
- addrSrv.sin_family=AF_INET;
- addrSrv.sin_port=htons(6000);
-
-
- bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
-
- listen(sockSrv,5);
-
- SOCKADDR_IN addrClient;
- int len=sizeof(SOCKADDR);
-
- printf("%s\n","welcome,the serve is started...");
- while (1)
- {
-
- SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
- char sendBuf[100];
- sprintf(sendBuf,"welcome %s to http://unblue2008.javaeye.com",inet_ntoa(addrClient.sin_addr));
-
-
- send(sockConn,sendBuf,100,0);
- char revBuf[100];
-
-
- recv(sockConn,revBuf,100,0);
-
- printf("%s\n",revBuf);
-
-
- closesocket(sockConn);
- }
- }
#include <Winsock2.h>
#include <stdio.h>
void main()
{
//加载套接字
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested=MAKEWORD(1,1);
err=WSAStartup(wVersionRequested,&wsaData);
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=htonl(INADDR_ANY);//把U_LONG的主机字节顺序转换为TCP/IP网络字节顺序
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
//绑定套接字
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
//将套接字设置为监听模式,准备接受用户请求
listen(sockSrv,5);
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
printf("%s\n","welcome,the serve is started...");
while (1)
{
//等待用户请求到来
SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);
char sendBuf[100];
sprintf(sendBuf,"welcome %s to http://unblue2008.javaeye.com",inet_ntoa(addrClient.sin_addr));
//发送数据
send(sockConn,sendBuf,100,0);
char revBuf[100];
//接收数据
recv(sockConn,revBuf,100,0);
//打印接受数据
printf("%s\n",revBuf);
//关闭套接字
closesocket(sockConn);
}
}
注意:需要包含头文件<Winsock2.h>,并且在工程设置的link里面加上ws32_2.dll
如果在VC中还有一个简单的加载套接字的方法:
- if (!AfxSocketInit())
- {
- AfxMessageBox("套接字加载失败!");
- return false;
- }
if (!AfxSocketInit())
{
AfxMessageBox("套接字加载失败!");
return false;
}
这个不需要包含上面注里面的头文件和ws2_32.lib库就可以实现加载套接字。
2、Socket客户端:
Socket客户端同样需要先加载套接字,然后创建套接字,不过之后不用绑定和监听了,而是直接连接服务器,发送相关请求。
同样贴出孙鑫VC详解里面的客户端的例子:(不是我偷懒,是人家实在写的太好,无法超越
)
- #include <Winsock2.h>
- #include <stdio.h>
-
- void main()
- {
-
- WORD wVersionRequested;
- WSADATA wsaData;
- int err;
-
- wVersionRequested=MAKEWORD(1,1);
-
- err=WSAStartup(wVersionRequested,&wsaData);
- if (err!=0)
- {
- return;
- }
-
- if (LOBYTE(wsaData.wVersion)!=1||
- HIBYTE(wsaData.wVersion)!=1)
- {
- WSACleanup();
- return;
- }
-
-
- SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
-
- SOCKADDR_IN addrSrv;
- addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
- addrSrv.sin_family=AF_INET;
- addrSrv.sin_port=htons(6000);
-
-
- connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
-
-
- char recBuf[100];
- recv(sockClient,recBuf,100,0);
- printf("%s\n",recBuf);
-
-
- send(sockClient,"this is 扈修非",strlen("this is 扈修非")+1,0);
-
- closesocket(sockClient);
- WSACleanup();
- }
#include <Winsock2.h>
#include <stdio.h>
void main()
{
//加载套接字
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested=MAKEWORD(1,1);
err=WSAStartup(wVersionRequested,&wsaData);
if (err!=0)
{
return;
}
if (LOBYTE(wsaData.wVersion)!=1||
HIBYTE(wsaData.wVersion)!=1)
{
WSACleanup();
return;
}
//创建套接字
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");//把U_LONG的主机字节顺序转换为TCP/IP网络字节顺序
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
//向服务器发送请求
connect(sockClient,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
//接受数据
char recBuf[100];
recv(sockClient,recBuf,100,0);
printf("%s\n",recBuf);
//发送数据
send(sockClient,"this is 扈修非",strlen("this is 扈修非")+1,0);
//关闭套接字
closesocket(sockClient);
WSACleanup();
}
需要加载的头文件和库同上
posted on 2009-12-15 23:24
fullfill 阅读(144)
评论(0) 编辑 收藏