write socket causes "program exited with code 141" in C -


I am trying to set up client / server in C. I establish the connection, then I want to send a user name to a PWD on the server, and the server must answer to confirm that it has received the USR / PWD. The problem is that both the server and the client leave the "written" or "reading" function as soon as they get it. What should I do?

server.c

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #include & lt; String.h & gt; # Include & lt; Unistd.h & gt; # Include & lt; arpa / inet.h & gt; # Include & lt; Sys / types.h & gt; #include & lt; Netinet / in.h> # Include & lt; Sys / socket.h & gt; # Include & lt; Netdb.h & gt; # Include & lt; Signal.h & gt; Zero 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_adr, sb (my_addr)) lt; 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); Hint (SIGCHLD, SIG_IGN); While (1) {if ((ds_sock_acc = acceptable (DS_sock, (structure supported) and ender, and sin_size)) 1) {printf ("accept error"); } Printf ("Connect"); Four users [10]; Read (ds_sock, usr, 10); Char * confirm_usr; Confirm_usr = "User name received"; Write (ds_sock, confirm_usr, 100); Four PWDs [10]; Read (ds_sock, PWD, 10); Char * confirm_pwd; Confirm_pwd = "The password has been received"; Write (ds_sock, confirm_pwd, 100); }}   

client.c:

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; #include & lt; String.h & gt; # Include & lt; Unistd.h & gt; # Include & lt; arpa / inet.h & gt; # Include & lt; Sys / types.h & gt; #include & lt; Netinet / in.h> # Include & lt; Sys / socket.h & gt; # Include & lt; Netdb.h & gt; Zero 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"); Exhaust (EXIT_FAILURE); } Printf ("Connect OK"); Four users [10]; Printf ("Enter username"); Scanf ("% s", usr); Four PWD [12]; Printf ("Enter password"); Scanf ("% s", Public Works Department); Printf ("% s", Public Works Department); Write (ds_sock, usr, 10); Four usr_reply [100]; Read (ds_sock, usr_reply, 100); Printf ("% s", usr_reply); Write (ds_sock, pwd, 12); Four pwd_reply [100]; Read (ds_sock, pwd_reply, 100); Printf ("% s", pwd_reply); }    

You are going out of range, thus writing your () call:

  four * confirm_usr; Confirm_usr = "User name received"; Write (ds_sock, confirm_usr, 100);   

You are asking write () to send 100 bytes, but you only provide an indicator for 18 bytes. Reading over 18 from that place triggers undetermined behavior, it will make more sense to use the strlen () to avoid the rigidity of the length:

  Const char * confirm_usr = "user name received"; Write (ds_sock, confirm_usr, strlen (confirm_usr) + 1); I have actually created  + 1  to terminate it  '\ 0' -  - Character, otherwise the other side of the other end   

In addition, you will have to check the return value of many calls I / O will fail and will fail and it will have to deal with it.

It seems too strange that it seems that both servers and clients start with read , and that the server, on the socket Ds_sock_acc is different from .

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -