套接字服务器程序中的运行时绑定错误

时间:2013-08-21 10:40:15

标签: c sockets

使用socket的服务器程序:

    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>  
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include<unistd.h>
    #define MYPORT 4444
    #define BACKLOG 100
    #define MAXBUFSIZE 100

    int main(void)
    {
        int sockfd,newfd,sin_size,i,count;
        struct sockaddr_in my_addr,their_addr; 
        char request[MAXBUFSIZE] = "This is the servers request";
        char buf[100];

        /*Create Socket */
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        printf("\nSocket created");

        my_addr.sin_family = AF_INET; 
        my_addr.sin_port = htons(MYPORT);
        my_addr.sin_addr.s_addr =inet_addr("10.228.37.9");

        /*Bind Socket*/
        i=bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
        if(i<0)
        {
            printf("\nBind Error");
            exit(1);
        }
        else
        {
            printf("\nBind socket");
        }

        /*Listen */
        listen(sockfd, BACKLOG) ;
        if( listen(sockfd, BACKLOG)==-1)
        {
            printf("\nError in listening");
         }
        else
            printf("Listened Successfully");

        sin_size = sizeof(struct sockaddr_in);

        /*Accept*/
        newfd = accept(sockfd, (struct sockaddr *)&their_addr,&sin_size);

        /*Receive from Client*/
        count = recv(newfd, buf, sizeof(buf), 0);
        if (count < 0) 
        {
            printf("recv error");
            exit(1);
        }
        buf[count]='\0';
        printf("\nReceived: %s",buf);
        printf("\nSuccessful");

        /*Send to Client*/ 
        if (send(newfd, request, (int)strlen(request), 0) < 0)
        {
            printf("send error");
            exit(1);
        }

        return 0;
    }

这是使用socket的c中的服务器程序, 没有编译错误,但在运行时程序应该等待客户端,但它显示绑定错误。 套接字服务器程序中的运行时绑定错误

1 个答案:

答案 0 :(得分:0)

修复“已使用的地址” 通过调用SO_REUSEADDR

设置标记setsocketopt

这只是一个小型演示如何重用端口

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
int main(){

//create a new socket stream
int sock_fd = socket(PF_INET, SOCK_STREAM, 0);
if (sock_fd == -1){
   printf("Socket Error %s\n", strerror(errno));
}
int reuse;
if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(int)) == -1){
 printf("Reuse port Error : %s\n", strerror(errno));
}
// configure the socket
struct sockaddr_in sock_addr;
 //bind to port 4500
sock_addr.sin_port = (in_port_t) htons(4500);
sock_addr.sin_family = PF_INET;
sock_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// bind the socket to given port
if (bind(sock_fd, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) == -1){
  printf("Bind Error %s\n", strerror(errno));
}

//listner to only 2 clients
 listen(sock_fd, 2);
// keep the connection running
 while(1){

 struct sockaddr_storage client_addr;

   // appect a connection from client
    unsigned int addr_size = sizeof(client_addr);
   int conn_fd = appect(sock_fd, (struct sockaddr *) &client_addr, &addr_size);

  char *msg = "Hello World";

  send(conn_fd, msg, strlen(msg), 0);
  close(conn_fd);
 } 
}