简体   繁体   English

在C Socket中发送和接收两个字符串

[英]Sending and receive two strings in C Socket

This is the client side where it prompts user to enter a string and send it to the server 这是客户端,它提示用户输入字符串并将其发送到服务器

//send
printf("\nPlaintext : ");
gets(send_data);
send(sock,send_data,strlen(send_data), 0);

//recv
bytes_recieved = recv(sock, recv_data, 1024, 0);
recv_data[bytes_recieved] = '\0';
printf("\nEnciphered text = %s " , recv_data);
fflush(stdout);
close(sock);

And this is the server side. 这是服务器端。

sin_size = sizeof(struct sockaddr_in);

//accept
connected = accept(sock, (struct sockaddr *)&client_addr,&sin_size);

printf("\n I got a connection from (%s , %d)", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

//recv
bytes_recieved = recv(connected, recv_data, 1024,0);

recv_data[bytes_recieved] = '\0';

printf("\n RECIEVED DATA = %s " , recv_data);

cod = encipher(recv_data, key, 1); //printf("Code: %s\n", cod);
dec = encipher(cod, key, 0); //printf("Back: %s\n", dec);

//send
send(connected, cod, strlen(cod), 0);
send(connected, dec, strlen(dec), 0);

So the thing is, I want to send two strings named 'plaintext' and 'key' from the client. 因此,我想从客户端发送两个名为“ plaintext”和“ key”的字符串。 At the server side, I expected it to receive the two strings and process it under encipher() function and then send it back to the client. 在服务器端,我希望它接收两个字符串并在encipher()函数下对其进行处理,然后将其发送回客户端。

How do I send two strings from a client and receive two strings from the server? 如何从客户端发送两个字符串并从服务器接收两个字符串?

I would send the string first then get the server to write back some confirmation to the client that it received it. 我将首先发送字符串,然后让服务器将一些确认写回给它接收到的客户端。 Then the client can send the key, the server can do what it needs to and return the message to the client. 然后,客户端可以发送密钥,服务器可以执行所需的操作,然后将消息返回给客户端。 If you keep the socket connection open you can continue to send and receive information from/to either end of the connection with send() and recv() . 如果保持套接字连接打开,则可以使用send()recv()继续在连接的任一端收发信息。 There are a number of ways depending on the structure of your strings. 有多种方法取决于字符串的结构。 You could use a delimiter in the data you send to the server to indicate the end of the string, so that the portion that remains is your key - this would allow the server to receive the data in one packet and reply in one packet. 您可以在发送到服务器的数据中使用定界符来指示字符串的结尾,这样剩下的部分就是您的密钥-这将允许服务器在一个数据包中接收数据并在一个数据包中进行回复。 I am not sure there is a right way to do it - you must decide based on the needs of your application. 我不确定是否有正确的方法-您必须根据应用程序的需求来决定。 Either way, you should check the return values of the socket functions to check that you are getting and sending what you think you are. 无论哪种方式,都应该检查套接字函数的返回值,以检查自己是否正在获取并发送自己认为的内容。

send a 16bit short containing the size of the 1st string, then the 1st string, then another short containing the size of the 2nd string then the 2nd string. 发送一个包含第一个字符串的大小的16位short,然后是第一个字符串,然后发送另一个包含第二个字符串的大小然后是第二个字符串的short。 On the server, read a 16bit int then read that many bytes, then do the same for the 2nd string. 在服务器上,读取一个16位的int,然后读取那么多字节,然后对第二个字符串执行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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