473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help me about socket port

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
1 1561
gpraghuram
1,275 Expert 1GB
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

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

Similar topics

2
by: Lindstrom Greg - glinds | last post by:
Hello- I have a python routine (Python 2.2.3 on Windows 2000 "Professional") using a socket connection. Problem is, the backend service I'm connecting to will hang every so often and the socket...
4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
4
by: DreJoh | last post by:
I've read many articles on the subject and the majority of them give the same solution that's in article 821625 on the MSDN website. I'm using the following code and when a the client disconnects...
3
by: Marcia Hon | last post by:
Hi, I am trying to use the select() socket programming command to select between stdin and a connection. Currently, I have a listening stream and stdin that I insert into the fd_set. The problem...
9
by: AA | last post by:
This is making me crazy!! Please, if some body can help me. I'm testing a ver simple socket client. In my test I just open and close a connection (in a loop) to my local IIS server (port 80)...
5
by: fjlpf | last post by:
Hi I am a beginner of programming. These days, I try to develop a ftp program. But I find it is harder than I have thought. I want to post my code, then concentrate on resolving the...
5
by: ranishobha21 | last post by:
Dear all, i want to send some unix commands to remote unix machine in france through php.i am using socket communication in php, i have written a socket communication program so that it...
2
by: manontheedge | last post by:
I've got two separate programs, one being the "server" and the other one (this one) being the "client". I've got the "server" to where it just sits and waits for connections, and sends out whatever...
6
by: zaina | last post by:
hi everybody i am nwebie in this forum but i think it is useful for me and the member are helpful my project is about connecting client with the server to start exchanging messages between...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.