客户端必须向服务器发送 2 个字符串,服务器必须向客户端发送回一个字符串,并且还要并行处理客户端。这两个实体在 Unix 下使用数据报交换数据。我的问题是,当我使用线程时,我无法将数据发送回客户端,也无法读取它。即使我不使用线程,我也无法正确地将消息发送回客户端。
服务器是:
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
int sd = 0;
void* worker(void* argm) {
struct sockaddr_in* adress = (struct sockaddr_in*) argm;
char s[100],s2[100];
int adressLen;
recvfrom(sd, s, sizeof(s), 0, (struct sockaddr*) &adress, &adressLen);
perror("recv");
Perror 显示 recv: 非套接字上的套接字操作
printf("I received: %s\n",s);
strcat(s2,"sample");
s2[strlen(s2)]='\0';
sendto(sd, &s2, sizeof(s2), 0, (struct sockaddr*) &adress,sizeof(adress));
perror("send");
}
最后perror()
显示发送:非套接字上的套接字操作。这也导致客户端无限等待来自服务器的字符串。
int main(int argc, char* argv[]) {
int sd;
int servPort;
struct sockaddr_in servAddr;
if (argc != 2){
printf("usage %s <port server>\n", argv[0]);
return 1;
}
if (1 != sscanf(argv[1], "%d", &servPort) || 0 > servPort || 0xffff < servPort){
printf("%s invalid port\n",argv[1]);
return 2;
}
servPort = htons(servPort);
sd = socket(AF_INET, SOCK_DGRAM, 0);
perror("socket: ");
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = servPort;
bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
perror("bind");
for(;;) {
char s[100];
struct sockaddr_in client;
client.sin_family=AF_INET;
int clientLen;
recvfrom(sd, s, sizeof(s), 0, (struct sockaddr*) &client, &clientLen);
perror("recv");
printf("I received: %s\n",s);
pthread_t thread;
pthread_create(&thread, NULL, worker, (void*) &client);
pthread_join(thread, NULL);
}
close(sd);
return 0;
}
控制台打印的内容:
bind: Success
recv: Success
I received: da
recv: Socket operation on non-socket
I received: `���
send: Socket operation on non-socket
recv: Success
I received: nu
recv: Socket operation on non-socket
I received: `���
send: Socket operation on non-socket
客户是:
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/ip.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int sd, r, lung, n;
struct sockaddr_in servAddr;
struct hostent *h;
int servPort;
if (argc != 3){
printf("usage %s <IP server> <port server>\n", argv[0]);
return 1;
}
h = gethostbyname(argv[1]);
if(h==NULL) {
printf("%s: unknown host '%s'\n",argv[0],argv[1]);
return 1;
}
if (1 != sscanf(argv[2], "%d", &servPort) || 0 > servPort || 0xffff < servPort){
printf("%s invalid port \n",argv[2]);
return 2;
}
servPort = htons(servPort);
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
servAddr.sin_port = servPort;
sd = socket(AF_INET,SOCK_DGRAM,0);
perror("socket ");
char s[100],s2[100],s3[100];
printf("enter text 1:\n");
fgets(s, 101, stdin);
printf("enter text 2:\n");
fgets(s2, 101, stdin);
s[strlen(s)] = '\0';
s2[strlen(s2)]='\0';
sendto(sd, &s, sizeof(s), 0, (struct sockaddr*) &servAddr, sizeof(servAddr));
perror("send");
sendto(sd, &s2, sizeof(s2), 0, (struct sockaddr*) &servAddr, sizeof(servAddr));
perror("send");
n = recvfrom(sd, s3, sizeof(s3), 0, (struct sockaddr*) &servAddr, &lung);
perror("recv");
printf("I received : %s", s3);
close(sd);
return 0;
}