+ -
当前位置:首页 → 问答吧 → 关于socket绑定的困惑,请xdjm们帮忙,十分紧急!!!

关于socket绑定的困惑,请xdjm们帮忙,十分紧急!!!

时间:2004-08-26

来源:互联网

说很多的话不如看源代码来的直接,我的源代码如下:
#include "string.h"
#include "netinet/in.h"
#include "netdb.h"
#include "sys/types.h"
#include "sys/socket.h"
#define MYPORT 3490


int main(int argc,char* argv[])
{
int sockfd,ret=0;
struct sockaddr_in my_addr={0};
sockfd=socket(AF_INET,SOCKET_STREAM,0);
if(sockfd==-1)
{printf("error new a socket");
  exit(0);
  }
  my_addr.sin_family=AF_INET;
  my_addr.sin_port=htons(MYPORT);
  my_addr.sin_addr.s_addr=inet_addr("166.241.5.11");
  bzero(&(my_addr.sin_zero),8);
  ret=bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr));
  if(ret==-1)
  {
    printf("error binding the socket\n");
    exit(0);
  }
  printf("binding successful!\n");
  exit(0);
  }

运行结果:error binding the socket
开始以为可能是设置的端口刚好是在用,用netstat察看,发现不是;换了几个端口,问题也没解决。希望大家给与帮助!      

作者: billwelch   发布时间: 2004-08-26

#include <string.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#define MYPORT 3490      

作者: billwelch   发布时间: 2004-08-26

热门下载

更多