基于UDP的windows 编程不能通信
//以下是client的代码#pragmacomment(lib,"ws2_32.lib")#include<windows.h>#include<winsock.h>#...
//以下是client的代码
#pragma comment(lib,"ws2_32.lib")
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
BOOL InitWinsock();
int main()
{ SOCKET socket1;//定义套接字
InitWinsock();
struct sockaddr_in server; //本地地址信息
int len=sizeof(server);
server.sin_family=AF_INET; //协议类型
server.sin_port=htons(1234); //绑定端口为 1000
server.sin_addr.s_addr=inet_addr("127.0.0.1"); //server IP
socket1=socket(AF_INET,SOCK_STREAM,0);
while (1)
{ char buffer[]="\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("receive from server:%s\n",buffer);
}
}
closesocket(socket1);
return 0;
}
BOOL InitWinsock()
{ int Error;
WORD VerRequested;
WSADATA Wsadata;
VerRequested=MAKEWORD(2,0);
Error=WSAStartup(VerRequested,&Wsadata);
if (Error!=0)
{ return FALSE;}
else
{ if (LOBYTE(Wsadata.wVersion)!=2||HIBYTE(Wsadata.wHighVersion)!=2)
{ WSACleanup();
return FALSE;
}
}
return TRUE;
}
// 以下是server的代码
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"WSOCK32.lib")
#include <windows.h>
#include <Winsock.h>
#include <stdio.h>
BOOL InitWinsock();
void main()
{ SOCKET socket1;
InitWinsock();
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen=sizeof(from);
local.sin_family=AF_INET;
local.sin_port=htons(1234);
local.sin_addr.s_addr=inet_addr("127.0.0.1");
socket1=socket(AF_INET,SOCK_DGRAM,0);//14:51 2013/4/2 星期二
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);
sendto(socket1,buffer,sizeof(buffer),0,(struct sockaddr *)&from,fromlen);
}
Sleep(5000);
}
closesocket(socket1);
}
BOOL InitWinsock()
{ int Error;
WORD VerRequested;
WSADATA Wsadata;
VerRequested=MAKEWORD(2,0);
Error=WSAStartup(VerRequested,&Wsadata);
if (Error!= 0)
{return FALSE;}
else
{ if (LOBYTE(Wsadata.wVersion)!=2||HIBYTE(Wsadata.wHighVersion)!=2)
{ WSACleanup();
return FALSE;
}
}
return TRUE;
} 展开
#pragma comment(lib,"ws2_32.lib")
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
BOOL InitWinsock();
int main()
{ SOCKET socket1;//定义套接字
InitWinsock();
struct sockaddr_in server; //本地地址信息
int len=sizeof(server);
server.sin_family=AF_INET; //协议类型
server.sin_port=htons(1234); //绑定端口为 1000
server.sin_addr.s_addr=inet_addr("127.0.0.1"); //server IP
socket1=socket(AF_INET,SOCK_STREAM,0);
while (1)
{ char buffer[]="\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("receive from server:%s\n",buffer);
}
}
closesocket(socket1);
return 0;
}
BOOL InitWinsock()
{ int Error;
WORD VerRequested;
WSADATA Wsadata;
VerRequested=MAKEWORD(2,0);
Error=WSAStartup(VerRequested,&Wsadata);
if (Error!=0)
{ return FALSE;}
else
{ if (LOBYTE(Wsadata.wVersion)!=2||HIBYTE(Wsadata.wHighVersion)!=2)
{ WSACleanup();
return FALSE;
}
}
return TRUE;
}
// 以下是server的代码
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"WSOCK32.lib")
#include <windows.h>
#include <Winsock.h>
#include <stdio.h>
BOOL InitWinsock();
void main()
{ SOCKET socket1;
InitWinsock();
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen=sizeof(from);
local.sin_family=AF_INET;
local.sin_port=htons(1234);
local.sin_addr.s_addr=inet_addr("127.0.0.1");
socket1=socket(AF_INET,SOCK_DGRAM,0);//14:51 2013/4/2 星期二
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);
sendto(socket1,buffer,sizeof(buffer),0,(struct sockaddr *)&from,fromlen);
}
Sleep(5000);
}
closesocket(socket1);
}
BOOL InitWinsock()
{ int Error;
WORD VerRequested;
WSADATA Wsadata;
VerRequested=MAKEWORD(2,0);
Error=WSAStartup(VerRequested,&Wsadata);
if (Error!= 0)
{return FALSE;}
else
{ if (LOBYTE(Wsadata.wVersion)!=2||HIBYTE(Wsadata.wHighVersion)!=2)
{ WSACleanup();
return FALSE;
}
}
return TRUE;
} 展开
2个回答
展开全部
你的Server和Client都在本地使用而且都是用1234端口怎么可能成功。要么把其中一个换到另一台机子上。要么换端口,两个不一样。建议创建完socket之后进行初始化判断,初始化成功在进行手法操作。
socket创建完后,和INVALID_SOCKET进行比较,多看看MSDN。
socket创建完后,和INVALID_SOCKET进行比较,多看看MSDN。
更多追问追答
追问
我换了端口,而且scoket的返回值也是正确的,但还是和先前的情况一样。为什么呢?
追答
不好意思刚才看错了,和端口没关系。我以为你在client上也bind了一个1234端口。
刚才调整了一下代码格式重新看了下。错误有两个:
1.你的服务端创建的是UDP而客户端创建的是TCP。
2.你的客户端在发送数据的时候,取字符长度应该用strlen而不是sizeof。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询