急需两个VC++的基于MFC对话框的Socket通讯(C/S简单即时通信)的代码,
2个回答
展开全部
服务器:
#include<stdio.h>
#include<winsock.h>
#pragma comment (lib,"wsock32.lib")
void main()
{
WSADATA data;
WSAStartup(MAKEWORD(2,0),&data);
SOCKET socket1;
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen =sizeof(from);
local.sin_family=AF_INET;
local.sin_port=htons(1000); ///监听端口
local.sin_addr.s_addr=INADDR_ANY; ///本机
socket1=socket(AF_INET,SOCK_DGRAM,0);
bind(socket1,(struct sockaddr*)&local,sizeof local);
while (1)
{
char buffer[1024]="\0";
printf("waiting for message from others-------------\n");
if (recvfrom(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&from,&fromlen)!=SOCKET_ERROR)
{
printf("Received datagram from %s--%s\n",inet_ntoa(from.sin_addr),buffer);
////给cilent发信息
sendto(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&from,fromlen);
}
}
closesocket(socket1);
WSACleanup();
}
客户端:
#include<stdio.h>
#include<winsock.h>
#pragma comment (lib,"wsock32.lib")
void main()
{
WSADATA data;
WSAStartup(MAKEWORD(2,0),&data);
SOCKET socket1;
struct sockaddr_in server;
int len =sizeof(server);
server.sin_family=AF_INET;
server.sin_port=htons(1000); ///server的监听端口
server.sin_addr.s_addr=inet_addr("???.???.???.???"); ///server的地址
socket1=socket(AF_INET,SOCK_DGRAM,0);
while (1)
{
char buffer[1024]="\0";
printf("input message\n");
scanf("%s",buffer);
if (strcmp(buffer,"bye")==0)
break;
if (sendto(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&server,len)!=SOCKET_ERROR)
{
if (recvfrom(socket1,
buffer,
sizeof buffer,
0,
(struct sockaddr*)&server,&len) != SOCKET_ERROR)
printf("rece from server:%s\n",buffer);
}
}
closesocket(socket1);
WSACleanup();
}
你照着里面的步骤就行啦,一样的
#include<stdio.h>
#include<winsock.h>
#pragma comment (lib,"wsock32.lib")
void main()
{
WSADATA data;
WSAStartup(MAKEWORD(2,0),&data);
SOCKET socket1;
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen =sizeof(from);
local.sin_family=AF_INET;
local.sin_port=htons(1000); ///监听端口
local.sin_addr.s_addr=INADDR_ANY; ///本机
socket1=socket(AF_INET,SOCK_DGRAM,0);
bind(socket1,(struct sockaddr*)&local,sizeof local);
while (1)
{
char buffer[1024]="\0";
printf("waiting for message from others-------------\n");
if (recvfrom(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&from,&fromlen)!=SOCKET_ERROR)
{
printf("Received datagram from %s--%s\n",inet_ntoa(from.sin_addr),buffer);
////给cilent发信息
sendto(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&from,fromlen);
}
}
closesocket(socket1);
WSACleanup();
}
客户端:
#include<stdio.h>
#include<winsock.h>
#pragma comment (lib,"wsock32.lib")
void main()
{
WSADATA data;
WSAStartup(MAKEWORD(2,0),&data);
SOCKET socket1;
struct sockaddr_in server;
int len =sizeof(server);
server.sin_family=AF_INET;
server.sin_port=htons(1000); ///server的监听端口
server.sin_addr.s_addr=inet_addr("???.???.???.???"); ///server的地址
socket1=socket(AF_INET,SOCK_DGRAM,0);
while (1)
{
char buffer[1024]="\0";
printf("input message\n");
scanf("%s",buffer);
if (strcmp(buffer,"bye")==0)
break;
if (sendto(socket1,buffer,sizeof buffer,0,(struct sockaddr*)&server,len)!=SOCKET_ERROR)
{
if (recvfrom(socket1,
buffer,
sizeof buffer,
0,
(struct sockaddr*)&server,&len) != SOCKET_ERROR)
printf("rece from server:%s\n",buffer);
}
}
closesocket(socket1);
WSACleanup();
}
你照着里面的步骤就行啦,一样的
追问
您费心了!可惜不是基于对话框的
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询