【笔记】VC++ 6.0 socket编程通信
本帖最后由 奋斗丶小Z 于 2015-11-11 18:34 编辑编程环境:VC++ 6.0
程序运行结果
服务端编写:
1,创建套接字
2,将套接字绑定到一个本地端口,并监听。
3,等待接收数据
4,关闭套接字
服务端代码:
//win32 console application
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib")
void main()
{
//初始化套接字,加载套接字
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested=MAKEWORD(2,2);
err=WSAStartup(wVersionRequested,&wsaData);
if(err!=0){
return;
}
//创建套接字
SOCKET sockSrv=socket(AF_INET,SOCK_DGRAM,0);
SOCKADDR_IN addrSrv={0};
addrSrv.sin_family=AF_INET;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_port=htons(1114);
//绑定套接字
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
//字符数组
char recvBuf; //保存接收信息
char sendBuf; //保存发送信息
char tempBuf; //存放中间临时数据
SOCKADDR_IN addrClient;
int len=sizeof(SOCKADDR);
while(1)
{
//等待并接收数据
recvfrom(sockSrv,recvBuf,100,0,(SOCKADDR*)&addrClient,&len);
if('q'==recvBuf)
{
sendto(sockSrv,"q",strlen("q")+1,0,(SOCKADDR*)&addrClient,len);
printf("chat end!\n");
break;
}
sprintf(tempBuf,"%s say:%s",inet_ntoa(addrClient.sin_addr),recvBuf);
printf("%s\n",tempBuf);
//发送数据
printf("please input data:\n");
gets(sendBuf);
sendto(sockSrv,sendBuf,strlen(sendBuf)+1,0,(SOCKADDR*)&addrClient,len);
}
//关闭套接字
closesocket(sockSrv);
WSACleanup();
}
客户端编写:
1,创建套接字
2,主动连接服务端
3,发送数据
4,关闭套接字
//win32 console application
#include<stdio.h>
#include<winsock2.h>
#pragma comment(lib,"ws2_32.lib")
void main()
{
//初始化套接字,加载套接字
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested=MAKEWORD(2,2);
err=WSAStartup(wVersionRequested,&wsaData);
if(err!=0){
return;
}
//创建套接字
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv={0};
addrSrv.sin_family=AF_INET;
addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrSrv.sin_port=htons(1114);
//字符数组
char recvBuf; //保存接收信息
char sendBuf; //保存发送信息
char tempBuf; //存放中间临时数据
int len=sizeof(SOCKADDR);
while(1)
{
//发送数据
printf("please input data:\n");
gets(sendBuf);
sendto(sockClient,sendBuf,strlen(sendBuf)+1,0,(SOCKADDR*)&addrSrv,len);
//等待并接收数据
recvfrom(sockClient,recvBuf,100,0,(SOCKADDR*)&addrSrv,&len);
if('q'==recvBuf)
{
sendto(sockClient,"q",strlen("q")+1,0,(SOCKADDR*)&addrSrv,len);
printf("chat end!\n");
break;
}
sprintf(tempBuf,"%s say:%s",inet_ntoa(addrSrv.sin_addr),recvBuf);
printf("%s\n",tempBuf);
}
//关闭套接字
closesocket(sockClient);
WSACleanup;
}
欢迎来编程语言区分享 这么好的帖子怎么没人呢,前几天学习套接字还专门到网上找资源来着 楼主发了基于UDP的,我就来一下基于TCP的吧:
服务器:#include <Winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv[])
{
//加载套接字库
WORD wVersionRequested;//WinSock库的版本号
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if( err != 0)
{
return 1 ;
}
if( LOBYTE( wsaData.wVersion) != 1 || HIBYTE( wsaData.wVersion) !=1 )
{
WSACleanup();
return -2;
}
//创建用于监听的套接字
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);//5为等待连续队列的最大值
SOCKADDR_IN addrClient;
int len = sizeof(SOCKADDR);
while(1)
{
//等待客户请求到来
SOCKET sockConn = accept(sockSrv, (SOCKADDR*)&addrClient, &len);//sockConn建立连接的套接字
char sendBuf;
sprintf( sendBuf, "Welcome %s to the FreeKing World", inet_ntoa(addrClient.sin_addr));
//发送数据
send( sockConn, sendBuf, strlen(sendBuf)+1, 0 );
char recvBuf;
//接受数据
recv( sockConn, recvBuf, 100, 0 );
//打印接受的数据
printf( "%s\n", recvBuf);
//关闭套接字
closesocket(sockConn);
}
return 0;
}
客户端:
#include <Winsock2.h>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
int main(int argc, char* argv[])
{
//加载套接字库
WORD wVersionRequested;//WinSock库的版本号
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if( err != 0)
{
return -1 ;
}
if( LOBYTE( wsaData.wVersion) != 1 || HIBYTE( wsaData.wVersion) !=1 )
{
WSACleanup();
return -2;
}
//创建套接字
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 recvBuf;
recv( sockClient, recvBuf, 100, 0 );
printf( "%s\n", recvBuf );
//发送数据
send(sockClient, "This is Dong Liu", strlen( "This is Dong Liu" )+1, 0 );
//关闭套接字
closesocket(sockClient);
WSACleanup();
return 0;
}
多好的东西了了,谢谢了!
多好的东西了了,谢谢了! 给你点32个赞 学习一下 TCP与UPD通信各有各的用途。
页:
[1]