C socket - connection refused at the second try -


I am creating a server / clint, but I have a problem if I boot the server and the client for the first time, So ever works fine then I close them, I boot them in a few seconds, and I get an e "connection denied" error in the client I always boot the server first, and the client After here the code is DoLogin 2 with reading and writing 2. The feat What's wrong with this code?

server.c

  int main () {int ds_sock; Struct sockaddr_in my_addr; Ds_sock = socket (AF_INET, SOCK_STREAM, 0); Memset (& my_addr, 0, sizeof (my_addr)); My_addr.sin_family = AF_INET; My_addr.sin_port = htons (25000); My_addr.sin_addr.s_addr = INADDR_ANY; If (bind (ds_sock, (structure sokadar *) and my add, size (my_addr)) <0} {printf ("error in bind"); } Listen (ds_sock, 2); Int ds_sock_acc; Struct sockaddr_in addr; Size_t sin_size = sizeof (struct sockaddr_in); If ((ds_sock_acc = Accept (DS-SOC, (Structure SOCADAR * * and Ender, & amp; sin_size)) <1) {printf ("Accepting the Error"); } Printf ("Connected \ n"); Char * usr = DoLogin (ds_sock_acc); Printf ("% s is connected \ n", USR); Close (ds_sock_acc); Close (ds_sock); Return 0; }   

client.c

 int main () {int ds_sock; Ds_sock = Socket (AF_INET, SOCK_STREAM, 0); Int rate; Straight soccer_year; Eaddr.sin_family = AF_INET; Ed. Sin_port = htons (25000); Eaddr.sin_addr.s_addr = inet_addr ("127.0.0.1"); Rate = Connect (DS-SOC, (Structure SOCADAR * *) and EDER, Size (EDD)); If (rate == - 1) {printf ("error connect"); Perror ("error:"); Exhaust (EXIT_FAILURE); } Printf ("Connect OK \ n"); Char * usr = DoLogin (ds_sock); Printf ("logged as% s \ n", usr); Close (ds_sock); Return 0; O / S usually prevents the hearing from being reproduced at the same port. Shortly after the last socket closure, to prevent messages from the original socket originally sent to the new socket.  

To override this behavior, you setockopt to the SO_REUSEADDR option (on the server side):

  int Optval = 1; Setochop (DS-SOC, SO_UUSEADDR, and Opal, Psychof (Opal));   

[Excluded check hidden]

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -