Sending data to socket programming using "C"

I used to ask a question regarding the same, but here I want a guide for my code. Using the advice from the people I tried to create to send the package. My maximum packet structure, along with the header and payload, is 16 bytes. If possible, take a look at the send and receive code and indicate where I am mistaken. Basically, my client continues to send data to the server, it just does not end, and the server does not show results.

Customer:

int main(int argc, char *argv[]) { int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; struct packet { long int srcID; long int destID; long int pver; long int profiles; char length; long int data; }; if (argc < 3) { fprintf(stderr,"usage: %s hostname port\n", argv[0]); exit(0); } portno = atoi(argv[2]); //Convert ASCII to integer sockfd = socket(AF_INET, SOCK_STREAM, 0); // socket file descriptor if (sockfd < 0) error("ERROR DETECTED !!! Problem in opening socket\n"); server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,"ERROR DETECTED !!!, no such server found \n"); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); //clear the memory for server address serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); printf("Client 1 trying to connect with server host %s on port %d\n", argv[1], portno); if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) error("ERROR in connection"); printf("SUCCESS !!! Connection established \n"); char buffer[128]; struct packet *pkt = (struct packet *) buffer; char *payload = buffer + sizeof(struct packet); long int packet_size; printf("Started Creating packet\n"); pkt->srcID = 0x01; pkt->destID = 0x02; pkt->pver = 0x01; pkt->profiles = 0x01; pkt->length = 128; pkt->data = 1; 2; 3; 4; 5; 6; 7; 8; if (send(sockfd,pkt,sizeof(packet_size),0) <0) printf ("error\n"); else printf ("packet send done"); return 0; } 

Server:

 int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen; struct sockaddr_in serv_addr, cli_addr; int n; char wish; long int SrcID; long int DestID; long int Pver; long int Profiles; long int Data; char Length; char bytes_to_receive; char received_bytes; struct packet { long int srcID; long int destID; long int pver; long int profiles; char length; long int data; }; if (argc < 2) { fprintf(stderr,"usage: %s port_number1",argv[0]); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("ERROR DETECTED !!! Problem in opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR DETECTED !!! There was a problem in binding"); listen(sockfd, 10); clilen = sizeof(cli_addr); printf("Server listening on port number %d...\n", serv_addr.sin_port); newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR DETECTED !!! the connection request was not accepted"); char buffer[128]; struct packet *pkt = (struct packet *) buffer; char *payload = buffer + sizeof(struct packet); long int packet_size; bytes_to_receive = sizeof(pkt); received_bytes = 0; if (recv(newsockfd, pkt, sizeof(pkt), 0) < 0) error("ERROR DETECTED !!! There was a problem in reading the data"); else { do { received_bytes += (buffer + received_bytes, bytes_to_receive - received_bytes); } while (received_bytes != bytes_to_receive); SrcID = pkt->srcID; DestID = pkt->destID; Pver = pkt->pver ; Profiles = pkt->profiles; Length = pkt->length; Data = pkt->data; printf("Data Received from Client_1 are :\n"); printf("Source ID: %l\n", SrcID); printf("Destination ID: %l\n", DestID); printf("profile Version: %l\n", Pver); printf("No of Profiles: %l\n", Profiles); printf("Length: %l\n", Length); printf("data : %l\n", Data); } if (close(newsockfd) == -1) { error("Error closing connection with client 1"); } printf("Connection with client 1 has been closed\n"); return 0; } 

Server does not show o / p. The client says that he sent the package. When compiling the server code, I see four warnings indicating unknown characters such as the 0xa conversion format for all printf statements in the server code. I think I'm wrong somewhere on the server side, but I can not follow the "serialization". Please update me with your input, it will be very useful.

+4
source share
3 answers

The problem with constantly sending the client is that you just use it in a loop. With a fixed indent, it becomes clear what happened:

 while (1) { if (send(sockfd,pkt,sizeof(packet_size),0) <0) printf ("error\n"); else printf ("packet send done"); } 
+3
source

Here are a few issues I found:

  • Your client continues to send packets because it is endless, loop.
  • You passed the wrong len parameter to recv. Right now you are passing sizeof (packet_size), which is equal to sizeof (long int) (4 bytes on a 32-bit OS), but probably your intention was to use sizeof (packet) (16 bytes).
  • You do not check how many bytes were actually read by recv. With TCP, you have no guarantee that you are reading all 16 bytes of the structured packet. Therefore, from time to time, you may read fewer bytes, and your packet will be incomplete. Here is an example in some pseudocode how you should get the whole package:

     bytes_to_receive = sizeof(packet) received_bytes = 0; do { received_bytes += recv(buffer + received_bytes, bytes_to_receive - received_bytes) } while (received_bytes != bytes_to_receive) 
  • Your packet structure on the client and server is different. In one, you use char length; in the second long int length;

  • I think that such assignments on the server do not make sense pkt->srcID = SrcID; and should be something like this SrcID = pkt->srcID;

+11
source
 addr_size = sizeof serverAddr; 

connect (clientSocket, (struct sockaddr *) & serverAddr, addr_size);

0
source

All Articles