无法发送用c编写的简单UDP数据包
can not send simple UDP packet written in c
我是一名正在学习套接字编程的 UNI 学生,我的第一个任务是创建一个简单的客户端服务器模型,客户端发送一个字符串,服务器将其转换为全部大写 returns。
我的代码没有收到任何错误,但是当我编译它时,服务器端在 for 循环的一次迭代后卡住了。
这是服务器端代码:
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define BUF_LEN 48
//this function will convert string into upper case
int processString(char *input, char *output){
int len = strlen(input);
for (int i=0;i<len;i++){
output[i] = toupper(input[i]);
}
output[len]='[=10=]';
return len;
}
int main(int argc,char *argv[]){
/* ----------------------------DECLARING VARIABLES---------------------------------- */
int ssd;
struct sockaddr_in server; //server info
struct sockaddr_in client; //client info
int client_len; // size of client
short echo_port; // the port number
int max_iterations; // usually runs forever but this will limit server
int byteInCount,byteOutCount,recieveCount,i;
char clientString[BUF_LEN];
char serverProcessedString[BUF_LEN];
int retCode;
/* ----------------------------END: DECLARING VARIABLES---------------------------------- */
/* ---------------------------CHECKING THE COMMAND LINE---------------------------- */
if (argc != 3)
{
fprintf(stderr,"incorrect usage! %s port Max_iterations\n",argv[0]);
exit(EXIT_FAILURE);
}
echo_port = atoi(argv[1]);
max_iterations = atoi(argv[2]);
/* ---------------------------END: CHECKING THE COMMAND LINE---------------------------- */
/* ----------------------------CREATE THE SOCKET CONNECTION---------------------------------- */
ssd = socket(PF_INET,SOCK_DGRAM,0); // refer to notes about meaning of each parameter
if (ssd <0){
perror("error in function socket()\n");
exit(EXIT_FAILURE);
}
server.sin_family=AF_INET; // ip protocol
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port= htonl(echo_port); // port to serve on
retCode = bind(ssd,(struct sockaddr *)&server,sizeof(server)); // ask tut about bind() function
if (retCode <0)
{
perror("error in function bind()\n");
exit(EXIT_FAILURE);
}
/* ----------------------------END: CREATE THE SOCKET CONNECTION---------------------------------- */
/* ---------------------------- START THE SERVER---------------------------------- */
for (int i=0;i<max_iterations;i++){
fprintf(stderr,"Iteration %d of %d. Waiting for client...\n",i+1, max_iterations);
client_len = sizeof(client);
byteInCount = recvfrom(ssd,clientString,BUF_LEN,0,(struct sockaddr *)&client,(socklen_t *)&client_len);
if (byteInCount <0)
{
perror("error in function revfrom()\n");
exit(EXIT_FAILURE);
}
else{
fprintf(stderr,"revfrom() is all g\n");
}
fprintf(stderr,"Reversed string is %d bytes long\n", recieveCount);
fprintf(stderr,"Reversed string is \"%s\"\n",serverProcessedString);
byteOutCount = sendto(ssd,serverProcessedString,recieveCount+1,0,(struct sockaddr *)&client,sizeof(client));
if (byteOutCount <0){
perror("error in function sendto()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"client request worked, reply has been sent\n");
}
close(ssd);
fprintf(stderr,"server has shutdown!\n");
return 0;
}
这是客户端代码:
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define BUF_LEN 48
int main(int argc,char *argv[]){
int csd;
struct sockaddr_in server;
struct hostent *server_host;
int serverLength;
int stringSize;
short serverPort;
int byteInCount,byteOutCount;
char clientString[BUF_LEN];
char serverProcessedString[BUF_LEN];
if (argc != 4){
fprintf(stderr,"usage: %s server port send_string \n",argv[0]);
exit(EXIT_FAILURE);
}
server_host =gethostbyname(argv[1]);
if (server_host== NULL){
herror("error in function gethostbyname()\n"); // ask tut- this function is no longer used( getaddrinfo)
exit(EXIT_FAILURE);
}
serverPort= atoi(argv[2]);
strcpy(clientString,argv[3]);
csd = socket(PF_INET,SOCK_DGRAM,0);
if(csd <0)
{
perror("error in function socket()");
exit(EXIT_FAILURE);
}
server.sin_family= AF_INET;
memcpy(&server.sin_addr.s_addr,server_host->h_addr_list[0],server_host->h_length); // ask tut about this function
server.sin_port=htons(serverPort); // difference between htons and htonl???
stringSize=strlen(clientString)+1;
byteOutCount= sendto(csd,clientString,stringSize,0,(struct sockaddr *)&server,sizeof(server));
if (byteOutCount <0){
perror("error in function sendto()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"You have sent \"%s\"\n",clientString);
fprintf(stderr,"Have reached recvfrom(), should now block until message receipt\n");
//get the response from server
serverLength = sizeof(server);
byteInCount = recvfrom(csd,serverProcessedString,BUF_LEN,0,(struct sockaddr *)&server,(socklen_t)&serverLength);
if (byteInCount <0){
perror("error in function recvfrom()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"the server has responded with: : \"%s\"\n",serverProcessedString);
close(csd);
return 0;
}
我正在 Ubuntu 虚拟机上编译:
1 号航站楼
cc UDP_client.c -o send
./send 192.168.0.1 9999 hello
(上面的ip地址是通过ifconfig命令获取的)
2 号航站楼
cc UDP_server.c -o rec
./rec 9999 10
我当前的输出:
2 号航站楼
"Iteration 1 of 10. waiting for client"
1 号航站楼:
"you have sent hello"
"have reached recvfrom(),should now block until message receipt"
我的循环似乎有问题?
任何帮助将不胜感激。
编辑:修复下面评论中的错误后,在客户端,我在 recvfrom 函数中收到错误:错误提示,地址错误?
我在您的代码中发现了一些问题。
- 确保结构 sockaddr_in 服务器和客户端应 memset 为零。因为这些是局部变量并且可以有无效值
- 在您的服务器中,您在调用 sendto 之前没有调用 processString()。
- 你的编译应该有零警告,这样你就可以解决很多固有的问题
- 缩进代码确实有助于提高可读性和可维护性。
- 添加了一些必需的 headers(unistd 和 stdlib.h)
- 已将客户端和服务器中的地址初始化为 INADDR_ANY,并为端口
初始化了 htons
- 一个建议,请将所有局部变量初始化为零以避免任何意外结果。
这里是服务器代码(注意,我保留了你的原始代码,尽可能接近原来的代码,稍作修改以使其工作,请彻底测试)
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#define BUF_LEN 48
//this function will convert string into upper case
int processString(char *input, char *output){
int len = strlen(input);
for (int i=0;i<len;i++){
output[i] = toupper(input[i]);
}
output[len]='[=10=]';
return len;
}
int main(int argc,char *argv[]){
/* ----------------------------DECLARING VARIABLES---------------------------------- */
int ssd;
struct sockaddr_in server; //server info
struct sockaddr_in client; //client info
int client_len; // size of client
short echo_port; // the port number
int max_iterations; // usually runs forever but this will limit server
int byteInCount,byteOutCount,recieveCount,i;
char clientString[BUF_LEN];
char serverProcessedString[BUF_LEN];
int retCode;
/* ----------------------------END: DECLARING VARIABLES---------------------------------- */
/* ---------------------------CHECKING THE COMMAND LINE---------------------------- */
if (argc != 3)
{
fprintf(stderr,"incorrect usage! %s port Max_iterations\n",argv[0]);
exit(EXIT_FAILURE);
}
echo_port = atoi(argv[1]);
max_iterations = atoi(argv[2]);
/* ---------------------------END: CHECKING THE COMMAND LINE---------------------------- */
/* ----------------------------CREATE THE SOCKET CONNECTION---------------------------------- */
ssd = socket(AF_INET, SOCK_DGRAM, 0); // refer to notes about meaning of each parameter
if (ssd <0){
perror("error in function socket()\n");
exit(EXIT_FAILURE);
}
memset(&server, 0, sizeof(server));
memset(&client, 0, sizeof(client));
server.sin_family=AF_INET; // ip protocol
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port= htons(echo_port); // port to serve on
retCode = bind(ssd,(struct sockaddr *)&server,sizeof(server)); // ask tut about bind() function
if (retCode <0)
{
perror("error in function bind()\n");
exit(EXIT_FAILURE);
}
/* ----------------------------END: CREATE THE SOCKET CONNECTION---------------------------------- */
/* ---------------------------- START THE SERVER---------------------------------- */
for (int i=0;i<max_iterations;i++){
fprintf(stderr,"Iteration %d of %d. Waiting for client...\n",i+1, max_iterations);
client_len = sizeof(client);
byteInCount = recvfrom(ssd, clientString, BUF_LEN,
MSG_WAITALL, (struct sockaddr *)&client,
(socklen_t *)&client_len);
if (byteInCount <0)
{
perror("error in function revfrom()\n");
exit(EXIT_FAILURE);
}
else{
fprintf(stderr,"revfrom() is all g\n");
}
recieveCount = processString(clientString, serverProcessedString);
fprintf(stderr,"Reversed string is %d bytes long\n", recieveCount);
fprintf(stderr,"Reversed string is \"%s\"\n",serverProcessedString);
byteOutCount = sendto(ssd, serverProcessedString, recieveCount+1, 0,
(struct sockaddr *)&client,sizeof(client));
if (byteOutCount <0){
perror("error in function sendto()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"client request worked, reply has been sent\n");
}
close(ssd);
fprintf(stderr,"server has shutdown!\n");
return 0;
}
这是您的客户端代码
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define BUF_LEN 48
int main(int argc,char *argv[]){
int csd;
struct sockaddr_in server;
struct hostent *server_host;
int serverLength;
int stringSize;
short serverPort;
int byteInCount,byteOutCount;
char clientString[BUF_LEN];
char serverProcessedString[BUF_LEN];
if (argc != 4){
fprintf(stderr,"usage: %s server port send_string \n",argv[0]);
exit(EXIT_FAILURE);
}
server_host =gethostbyname(argv[1]);
if (server_host== NULL){
herror("error in function gethostbyname()\n"); // ask tut- this function is no longer used( getaddrinfo)
exit(EXIT_FAILURE);
}
serverPort= atoi(argv[2]);
strcpy(clientString,argv[3]);
csd = socket(AF_INET, SOCK_DGRAM, 0);
if(csd <0)
{
perror("error in function socket()");
exit(EXIT_FAILURE);
}
memset(&server, 0, sizeof(server));
server.sin_family= AF_INET;
//memcpy(&server.sin_addr.s_addr,server_host->h_addr_list[0],server_host->h_length); // ask tut about this function
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(serverPort); // difference between htons and htonl???
stringSize = strlen(clientString)+1;
byteOutCount= sendto(csd, clientString, stringSize,
MSG_CONFIRM, (struct sockaddr *)&server,
sizeof(server));
if (byteOutCount <0){
perror("error in function sendto()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"You have sent \"%s\"\n",clientString);
fprintf(stderr,"Have reached recvfrom(), should now block until message receipt\n");
//get the response from server
serverLength = sizeof(server);
byteInCount = recvfrom(csd, serverProcessedString, BUF_LEN,
MSG_WAITALL, (struct sockaddr *)&server,
(socklen_t *) &serverLength);
if (byteInCount <0){
perror("error in function recvfrom()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"the server has responded with: : \"%s\"\n",serverProcessedString);
close(csd);
return 0;
}
在一个终端中,运行
./服务器 9999 10
在另一个终端运行
./client localhost 9999 你好
服务器端输出
Iteration 1 of 10. Waiting for client...
revfrom() is all g
Reversed string is 5 bytes long
Reversed string is "HELLO"
client request worked, reply has been sent
Iteration 2 of 10. Waiting for client...
客户端输出
You have sent "hello"
Have reached recvfrom(), should now block until message receipt
the server has responded with: : "HELLO"
祝一切顺利。我没有检查过你的代码,确保 processString 工作正常。祝一切顺利!
我是一名正在学习套接字编程的 UNI 学生,我的第一个任务是创建一个简单的客户端服务器模型,客户端发送一个字符串,服务器将其转换为全部大写 returns。
我的代码没有收到任何错误,但是当我编译它时,服务器端在 for 循环的一次迭代后卡住了。
这是服务器端代码:
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define BUF_LEN 48
//this function will convert string into upper case
int processString(char *input, char *output){
int len = strlen(input);
for (int i=0;i<len;i++){
output[i] = toupper(input[i]);
}
output[len]='[=10=]';
return len;
}
int main(int argc,char *argv[]){
/* ----------------------------DECLARING VARIABLES---------------------------------- */
int ssd;
struct sockaddr_in server; //server info
struct sockaddr_in client; //client info
int client_len; // size of client
short echo_port; // the port number
int max_iterations; // usually runs forever but this will limit server
int byteInCount,byteOutCount,recieveCount,i;
char clientString[BUF_LEN];
char serverProcessedString[BUF_LEN];
int retCode;
/* ----------------------------END: DECLARING VARIABLES---------------------------------- */
/* ---------------------------CHECKING THE COMMAND LINE---------------------------- */
if (argc != 3)
{
fprintf(stderr,"incorrect usage! %s port Max_iterations\n",argv[0]);
exit(EXIT_FAILURE);
}
echo_port = atoi(argv[1]);
max_iterations = atoi(argv[2]);
/* ---------------------------END: CHECKING THE COMMAND LINE---------------------------- */
/* ----------------------------CREATE THE SOCKET CONNECTION---------------------------------- */
ssd = socket(PF_INET,SOCK_DGRAM,0); // refer to notes about meaning of each parameter
if (ssd <0){
perror("error in function socket()\n");
exit(EXIT_FAILURE);
}
server.sin_family=AF_INET; // ip protocol
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port= htonl(echo_port); // port to serve on
retCode = bind(ssd,(struct sockaddr *)&server,sizeof(server)); // ask tut about bind() function
if (retCode <0)
{
perror("error in function bind()\n");
exit(EXIT_FAILURE);
}
/* ----------------------------END: CREATE THE SOCKET CONNECTION---------------------------------- */
/* ---------------------------- START THE SERVER---------------------------------- */
for (int i=0;i<max_iterations;i++){
fprintf(stderr,"Iteration %d of %d. Waiting for client...\n",i+1, max_iterations);
client_len = sizeof(client);
byteInCount = recvfrom(ssd,clientString,BUF_LEN,0,(struct sockaddr *)&client,(socklen_t *)&client_len);
if (byteInCount <0)
{
perror("error in function revfrom()\n");
exit(EXIT_FAILURE);
}
else{
fprintf(stderr,"revfrom() is all g\n");
}
fprintf(stderr,"Reversed string is %d bytes long\n", recieveCount);
fprintf(stderr,"Reversed string is \"%s\"\n",serverProcessedString);
byteOutCount = sendto(ssd,serverProcessedString,recieveCount+1,0,(struct sockaddr *)&client,sizeof(client));
if (byteOutCount <0){
perror("error in function sendto()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"client request worked, reply has been sent\n");
}
close(ssd);
fprintf(stderr,"server has shutdown!\n");
return 0;
}
这是客户端代码:
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define BUF_LEN 48
int main(int argc,char *argv[]){
int csd;
struct sockaddr_in server;
struct hostent *server_host;
int serverLength;
int stringSize;
short serverPort;
int byteInCount,byteOutCount;
char clientString[BUF_LEN];
char serverProcessedString[BUF_LEN];
if (argc != 4){
fprintf(stderr,"usage: %s server port send_string \n",argv[0]);
exit(EXIT_FAILURE);
}
server_host =gethostbyname(argv[1]);
if (server_host== NULL){
herror("error in function gethostbyname()\n"); // ask tut- this function is no longer used( getaddrinfo)
exit(EXIT_FAILURE);
}
serverPort= atoi(argv[2]);
strcpy(clientString,argv[3]);
csd = socket(PF_INET,SOCK_DGRAM,0);
if(csd <0)
{
perror("error in function socket()");
exit(EXIT_FAILURE);
}
server.sin_family= AF_INET;
memcpy(&server.sin_addr.s_addr,server_host->h_addr_list[0],server_host->h_length); // ask tut about this function
server.sin_port=htons(serverPort); // difference between htons and htonl???
stringSize=strlen(clientString)+1;
byteOutCount= sendto(csd,clientString,stringSize,0,(struct sockaddr *)&server,sizeof(server));
if (byteOutCount <0){
perror("error in function sendto()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"You have sent \"%s\"\n",clientString);
fprintf(stderr,"Have reached recvfrom(), should now block until message receipt\n");
//get the response from server
serverLength = sizeof(server);
byteInCount = recvfrom(csd,serverProcessedString,BUF_LEN,0,(struct sockaddr *)&server,(socklen_t)&serverLength);
if (byteInCount <0){
perror("error in function recvfrom()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"the server has responded with: : \"%s\"\n",serverProcessedString);
close(csd);
return 0;
}
我正在 Ubuntu 虚拟机上编译: 1 号航站楼
cc UDP_client.c -o send
./send 192.168.0.1 9999 hello
(上面的ip地址是通过ifconfig命令获取的)
2 号航站楼
cc UDP_server.c -o rec
./rec 9999 10
我当前的输出:
2 号航站楼
"Iteration 1 of 10. waiting for client"
1 号航站楼:
"you have sent hello"
"have reached recvfrom(),should now block until message receipt"
我的循环似乎有问题? 任何帮助将不胜感激。
编辑:修复下面评论中的错误后,在客户端,我在 recvfrom 函数中收到错误:错误提示,地址错误?
我在您的代码中发现了一些问题。
- 确保结构 sockaddr_in 服务器和客户端应 memset 为零。因为这些是局部变量并且可以有无效值
- 在您的服务器中,您在调用 sendto 之前没有调用 processString()。
- 你的编译应该有零警告,这样你就可以解决很多固有的问题
- 缩进代码确实有助于提高可读性和可维护性。
- 添加了一些必需的 headers(unistd 和 stdlib.h)
- 已将客户端和服务器中的地址初始化为 INADDR_ANY,并为端口 初始化了 htons
- 一个建议,请将所有局部变量初始化为零以避免任何意外结果。
这里是服务器代码(注意,我保留了你的原始代码,尽可能接近原来的代码,稍作修改以使其工作,请彻底测试)
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#define BUF_LEN 48
//this function will convert string into upper case
int processString(char *input, char *output){
int len = strlen(input);
for (int i=0;i<len;i++){
output[i] = toupper(input[i]);
}
output[len]='[=10=]';
return len;
}
int main(int argc,char *argv[]){
/* ----------------------------DECLARING VARIABLES---------------------------------- */
int ssd;
struct sockaddr_in server; //server info
struct sockaddr_in client; //client info
int client_len; // size of client
short echo_port; // the port number
int max_iterations; // usually runs forever but this will limit server
int byteInCount,byteOutCount,recieveCount,i;
char clientString[BUF_LEN];
char serverProcessedString[BUF_LEN];
int retCode;
/* ----------------------------END: DECLARING VARIABLES---------------------------------- */
/* ---------------------------CHECKING THE COMMAND LINE---------------------------- */
if (argc != 3)
{
fprintf(stderr,"incorrect usage! %s port Max_iterations\n",argv[0]);
exit(EXIT_FAILURE);
}
echo_port = atoi(argv[1]);
max_iterations = atoi(argv[2]);
/* ---------------------------END: CHECKING THE COMMAND LINE---------------------------- */
/* ----------------------------CREATE THE SOCKET CONNECTION---------------------------------- */
ssd = socket(AF_INET, SOCK_DGRAM, 0); // refer to notes about meaning of each parameter
if (ssd <0){
perror("error in function socket()\n");
exit(EXIT_FAILURE);
}
memset(&server, 0, sizeof(server));
memset(&client, 0, sizeof(client));
server.sin_family=AF_INET; // ip protocol
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port= htons(echo_port); // port to serve on
retCode = bind(ssd,(struct sockaddr *)&server,sizeof(server)); // ask tut about bind() function
if (retCode <0)
{
perror("error in function bind()\n");
exit(EXIT_FAILURE);
}
/* ----------------------------END: CREATE THE SOCKET CONNECTION---------------------------------- */
/* ---------------------------- START THE SERVER---------------------------------- */
for (int i=0;i<max_iterations;i++){
fprintf(stderr,"Iteration %d of %d. Waiting for client...\n",i+1, max_iterations);
client_len = sizeof(client);
byteInCount = recvfrom(ssd, clientString, BUF_LEN,
MSG_WAITALL, (struct sockaddr *)&client,
(socklen_t *)&client_len);
if (byteInCount <0)
{
perror("error in function revfrom()\n");
exit(EXIT_FAILURE);
}
else{
fprintf(stderr,"revfrom() is all g\n");
}
recieveCount = processString(clientString, serverProcessedString);
fprintf(stderr,"Reversed string is %d bytes long\n", recieveCount);
fprintf(stderr,"Reversed string is \"%s\"\n",serverProcessedString);
byteOutCount = sendto(ssd, serverProcessedString, recieveCount+1, 0,
(struct sockaddr *)&client,sizeof(client));
if (byteOutCount <0){
perror("error in function sendto()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"client request worked, reply has been sent\n");
}
close(ssd);
fprintf(stderr,"server has shutdown!\n");
return 0;
}
这是您的客户端代码
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define BUF_LEN 48
int main(int argc,char *argv[]){
int csd;
struct sockaddr_in server;
struct hostent *server_host;
int serverLength;
int stringSize;
short serverPort;
int byteInCount,byteOutCount;
char clientString[BUF_LEN];
char serverProcessedString[BUF_LEN];
if (argc != 4){
fprintf(stderr,"usage: %s server port send_string \n",argv[0]);
exit(EXIT_FAILURE);
}
server_host =gethostbyname(argv[1]);
if (server_host== NULL){
herror("error in function gethostbyname()\n"); // ask tut- this function is no longer used( getaddrinfo)
exit(EXIT_FAILURE);
}
serverPort= atoi(argv[2]);
strcpy(clientString,argv[3]);
csd = socket(AF_INET, SOCK_DGRAM, 0);
if(csd <0)
{
perror("error in function socket()");
exit(EXIT_FAILURE);
}
memset(&server, 0, sizeof(server));
server.sin_family= AF_INET;
//memcpy(&server.sin_addr.s_addr,server_host->h_addr_list[0],server_host->h_length); // ask tut about this function
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(serverPort); // difference between htons and htonl???
stringSize = strlen(clientString)+1;
byteOutCount= sendto(csd, clientString, stringSize,
MSG_CONFIRM, (struct sockaddr *)&server,
sizeof(server));
if (byteOutCount <0){
perror("error in function sendto()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"You have sent \"%s\"\n",clientString);
fprintf(stderr,"Have reached recvfrom(), should now block until message receipt\n");
//get the response from server
serverLength = sizeof(server);
byteInCount = recvfrom(csd, serverProcessedString, BUF_LEN,
MSG_WAITALL, (struct sockaddr *)&server,
(socklen_t *) &serverLength);
if (byteInCount <0){
perror("error in function recvfrom()\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"the server has responded with: : \"%s\"\n",serverProcessedString);
close(csd);
return 0;
}
在一个终端中,运行 ./服务器 9999 10
在另一个终端运行 ./client localhost 9999 你好
服务器端输出
Iteration 1 of 10. Waiting for client...
revfrom() is all g
Reversed string is 5 bytes long
Reversed string is "HELLO"
client request worked, reply has been sent
Iteration 2 of 10. Waiting for client...
客户端输出
You have sent "hello"
Have reached recvfrom(), should now block until message receipt
the server has responded with: : "HELLO"
祝一切顺利。我没有检查过你的代码,确保 processString 工作正常。祝一切顺利!