- 论坛徽章:
- 0
|
我在linux下了个UDP程序。我想使192.168.96.245和192.168.96.13两台机通信,它们运行的是同一个程序,只是地址不同而已。但运行的时候会报如下错误:
connect: Cannot assign requested address
我的192.168.96.245代码如下:
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define SERVER_PORT 5555
main()
{
int peerSocket;
struct sockaddr_in self_addr;
struct sockaddr_in peer_Addr;
int peer_len = sizeof(peer_Addr);
char recvbuf[1024];
char sendbuf[1024];
if((peerSocket = socket(AF_INET,SOCK_DGRAM,0)) < 0)
{
perror("socket");
exit(1);
}
bzero(&self_addr,sizeof(self_addr));
self_addr.sin_family =AF_INET;
self_addr.sin_port = htons(SERVER_PORT);
self_addr.sin_addr.s_addr = inet_addr("192.168.96.13");
if(bind(peerSocket,(struct sockaddr *)&self_addr,sizeof(self_addr)) < 0)
{
perror("connect");
exit(1);
}
while(1)scanf("%s",sendbuf);
printf("\n");
sendto(peerSocket,sendbuf,1024,0,(struct sockaddr *)&self_addr,sizeof(self_addr));
recvfrom(peerSocket,recvbuf,1024,0,(struct sockaddr *)&peer_Addr,(socklen_t*)&peer_len);
printf("recv from %s\n",recvbuf);
}
}
还有,如果把上面192.168.96.13地址改成127.0.0.1,程序可以正常运行,只不过是自己跟跟通信,本人是初学者,希望各位前辈指教下。谢谢
{
printf("Input your World:>"); |
|