By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,534 Members | 1,807 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,534 IT Pros & Developers. It's quick & easy.

Help me about socket port

P: 1
My project must send data from server to client via socket which implement by tcp socket.Now it is working but there is a one big problem that when the server program end , I must run the server again immediately after it end but it can't run agian because of port didn't available. So have any solutions to make the port available.

this is mycode
============================================
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <netinet/in.h>
  7. #include <sys/socket.h>
  8. #include <sys/wait.h>
  9.  
  10. #define MYPORT 51111
  11. #define BACKLOG 10 
  12. #define MAXDATASIZE 100
  13.  
  14. int main() {
  15.     char *input;
  16.     char buf[MAXDATASIZE];
  17.     int sockfd , new_fd;
  18.     struct sockaddr_in my_addr;
  19.     struct sockaddr_in their_addr;
  20.     int sin_size;
  21.     if((sockfd = socket(AF_INET,SOCK_STREAM,0))== -1 ){
  22.         perror("socket");
  23.         exit(1);
  24.     }
  25.     my_addr.sin_family = AF_INET;
  26.     my_addr.sin_port = htons(MYPORT);
  27.     my_addr.sin_addr.s_addr = INADDR_ANY;
  28.  
  29.     bzero( &(my_addr.sin_zero), 8 );
  30.     if(bind(sockfd, (struct sockaddr *)&my_addr,sizeof(struct sockaddr))== -1 ){
  31.         perror("bind");
  32.         exit(1);
  33.     }
  34.     if(listen(sockfd, BACKLOG)== -1){
  35.         perror("listen");
  36.         exit(1);
  37.     }
  38.  
  39.     while(1){
  40.  
  41.         sin_size = sizeof(struct sockaddr_in);
  42.         if((new_fd=accept(sockfd,(struct sockaddr *)&their_addr, &sin_size))== -1){
  43.             perror("accept");
  44.             continue;
  45.         }
  46.         char *str=inet_ntoa(their_addr.sin_addr);
  47.         printf("server: got connection from %s\n",&str);
  48.  
  49.         FILE *fpt;
  50.         char data[78];
  51.         if ((fpt = fopen("/var/www/slab.dx","r"))==NULL){
  52.         printf("Cannot open file.\n");
  53.         }else{
  54.         while( fgets(data,sizeof(data),fpt) != NULL){
  55.             send(new_fd,data,strlen(data),0);
  56.         }
  57.         fclose(fpt);
  58.         }
  59.         close(new_fd);
  60.     }
  61.     return 0;
  62. }
============================================
Jan 12 '08 #1
Share this Question
Share on Google+
1 Reply


gpraghuram
Expert 100+
P: 1,275
In server side after terminating the connection with the client you should use shutdown() to terminate the connection from the server side.
If you use it then the port will be freed immediately.

Raghuram
Jan 23 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.