简体   繁体   中英

Sending data to and from using sockets in c

I am working on an program for school and having some issues with sockets. I have pasted the write and read commands from my program below since I think these are the problem. The program should take the plaintext file and encrypt it using the key provided.

MY PROBLEM: When I execute the program using "client [plaintext] [key] [port]" the program returns "Reading data from client -- 140 bytes" and then just hangs. I can hit ctrl-c and the program prints the correct output for ptext and ktext and that 37 bytes were sent back to the client (which is the correct number of bytes). I feel like the encrypted text should print as well but it does not.

TWO QUESTIONS:

1) Why does the program hang?

2) Why does it seem like data is written from the server to the client but the client does not read any of the data?

Thank you in advance for any help you can offer.

CLIENT

n = write(sockfd,ptext,strlen(ptext));

bzero(crypt_text, BUF_MAX);
bzero(buffer, BUF_MAX);

while((n = read(sockfd,buffer,BUF_MAX))>0){
  printf("Reading data from Server -- %d bytes\n",n);
  strcat(crypt_text, buffer);
  bzero(buffer,BUF_MAX);
}
if (n < 0){
  error("ERROR reading from socket");
}

printf("%s", crypt_text);

SERVER

while((n = read(newsockfd,buffer,512))>0){
  printf("Reading data from client -- %d bytes\n",n);
  strcat(full_text, buffer);
  bzero(buffer,BUF_MAX);
}
if (n < 0){
  error("ERROR reading from socket");
}

bzero (ptext,BUF_MAX);
bzero (ktext, BUF_MAX);
strcpy(ptext, strtok(full_text,"["));
strcpy(ktext, strtok(NULL, "["));

printf("ptext length ==%s %d\n\n",ptext,strlen(ptext)); //Prints the correct     plain text 
printf("ktext length ==%s %d\n\n",ktext,strlen(ktext)); //prints the correct key

crypt_text = encrypt(ptext, ktext);

n = write(newsockfd,crypt_text,strlen(crypt_text));
printf("WRITE TO CILENT ==== %d",n); //This returns the correct number of     bytes that should be sent back to client

if (n < 0){
  error("ERROR writing to socket");
}

As is, your client and server will always hang waiting for each other. This is because read() blocks by default until new data is available to fetch from the file (in this case, a socket).

Look carefully at the code:

  1. The client writes once into the socket before entering the read loop
  2. The server only reads from the socket (well, further down there is a write() , but it will never reach it). The first time the loop runs on the server, it will read the data that the client initially wrote into the socket.
  3. The server processes the data it just read and concatenates it to full_text . Then it goes back to the loop condition, where it calls read() again. read() blocks because there is nothing else to read from the socket at this point.
  4. The client enters a similar loop where it attempts to read from the socket, expecting messages from the server.
  5. At this point, both the server and the client are blocked waiting for messages from each other, which will never happen.

Tu put it another way: you only wrote to the socket once, and somehow you expect to read it multiple times.

You have to rethink your design. Go back to the problem description, work your way through a simple protocol, dry run it on paper, and then implement it - that's how it's done in the real world :)

There are other bugs in your code. For example you write this:

 strcat(full_text, buffer);

But buffer is not NUL terminated. n bytes have been read, the rest of the buffer is indeterminate. You should set a '\\0' byte at offset n and only try reading BUF_MAX-1 bytes to keep a byte available for all cases and do this:

buffer[n] = '\0';
strcat(full_text, buffer);

Furthermore, you do not test if there is enough room available in full_text for the n+1 bytes strcat will copy at the end.

On another front, packets can be sliced and diced into chunked of different sizes when received by the server. Buffering is required to ensure a reliable client / server communication. To enable this buffering, you need to devise a protocol to determine when a packet has been fully received: a simple protocol is to transmit lines terminated by '\\n' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM