2 zuozhi305 zuozhi305 于 2013.09.12 16:40 提问

bind,address already in use

设置了setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on),任然解决不了问题!
代码如下:

/* 接收方代码 */
#include
#include
#include
#include
#include
#include
#include

struct sockaddr myname;
struct sockaddr from_name;
char buf[80];
main()
{
int sock;
int fromlen, cnt;
int on = 1;
sock = socket(AF_UNIX, SOCK_DGRAM, 0);
if (sock < 0)
{
printf("socket failure %d\n", errno);
exit(1);
}
myname.sa_family = AF_UNIX;
strcpy(myname.sa_data, "/tmp/aaa");
if (setsockopt( sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0)
{
perror("Server setsockopt failed");
return 1;
}

if (bind(sock, &myname, strlen(myname.sa_data) +
         sizeof(myname.sa_family)) < 0)
{
    perror("bind failure:");
    exit(1);
}               /* 建立套接字 */
cnt = recvfrom(sock, buf, sizeof(buf),
               0, &from_name, &fromlen);
if (cnt < 0)
{
    perror("recvfrom failure:");
    exit(1);
}               /* 调用recvfrom函数从套接字接收数据 */
buf[cnt] = '\0';  /* 添加字符串结束标志 */
from_name.sa_data[fromlen] = '\0';
printf("'%s' received from %s\n",
       buf, from_name.sa_data);

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!