所以我有一個非常簡單的多服務器客戶端應用程序(Linux)。我試圖做的初學者是連接到服務器接收來自它的消息,並寫入消息到服務器,然後是回聲。客戶端在新形成的連接上沒有收到消息
問題是,在連接而不是來自服務器的問候消息時,客戶端已準備好將消息鍵入終端 - 沒有問候消息。在輸入任何我想要的信息之後,我會回覆問候信息!我在這裏做錯了什麼?下面的代碼。
server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <pthread.h>
#include <netdb.h>
#include <netinet/in.h>
int server(int client_socket);
void* handle_connection(void* inc);
int main(int argc, char* argv[])
{
int socket_fd;
int client_socket_fd;
int port_number;
int client_length;
int * new_sock;
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
char buffer[256];
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd < 0) {
perror("Error opening socket!");
exit(1);
}
/* Initialize socket structure */
port_number = 1234;
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr= INADDR_ANY;
serv_addr.sin_port = htons(port_number);
/* Binding */
if(bind(socket_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("Error on binding!");
exit(1);
}
listen(socket_fd, 10);
client_length = sizeof(cli_addr);
while(client_socket_fd = accept(socket_fd, (struct sockaddr*) &cli_addr, &client_length)) {
pthread_t thread_id;
new_sock = malloc(sizeof(client_socket_fd));
*new_sock = client_socket_fd;
if(pthread_create(&thread_id, NULL, handle_connection, (void*) new_sock)) {
perror("could not create thread");
exit(1);
}
pthread_join(thread_id, NULL);
}
if(client_socket_fd < 0) {
perror("Error on accept client");
exit(1);
}
return 0;
}
void* handle_connection(void* inc)
{
int socket_fd = *(int *) inc;
int message_size;
char *message;
char buffer[256];
message = "You have been connected\n";
write(socket_fd, message, strlen(message));
message = "I will repeat what you type\n";
write(socket_fd, message, strlen(message));
while((message_size = recv(socket_fd, buffer, sizeof(buffer)/sizeof(buffer[0]), 0)) > 0) {
write(socket_fd, buffer, strlen(buffer));
}
if(message_size == 0) {
puts("Client disconnected");
fflush(stdout);
}
else if(message_size == -1) {
perror("Reading back from client failed");
}
free(inc);
return 0;
}
client.c
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <pthread.h>
int main(int argc, char* argv[])
{
int socket_fd, port_number;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if(argc<2) {
fprintf(stderr,"Incorrect arguments input\n");
exit(0);
}
//port_number = atoi(argv[2]);
port_number = 1234;
server = gethostbyname(argv[1]);
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if(socket_fd < 0) {
perror("Error opening socket");
exit(1);
}
bzero((char*) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port_number);
bcopy((char*) server->h_addr,(char *) &serv_addr.sin_addr.s_addr,sizeof(server->h_length));
if(connect(socket_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("Error connecting");
exit(1);
}
while(1) {
bzero(buffer, sizeof(buffer)/sizeof(buffer[0]));
printf("Enter the message: ");
bzero(buffer, sizeof(buffer)/sizeof(buffer[0]));
fgets(buffer, sizeof(buffer)/sizeof(buffer[0]) - 1, stdin);
if(send(socket_fd, buffer, strlen(buffer), 0) < 0) {
perror("Error sending message");
exit(1);
}
bzero(buffer, sizeof(buffer)/sizeof(buffer[0]));
if(recv(socket_fd, buffer, sizeof(buffer), 0) < 0) {
perror("Error reading back from server");
exit(1);
}
printf("Server reply: ");
printf("%s",buffer);
}
close(socket_fd);
return 0;
}
服務器回覆應在例如:HEJ。
客戶端連接後,也有2個消息從服務器。在開始輸入循環之前,您必須閱讀這些內容。 –
@JohnnyMopp我該怎麼做?我不知道從服務器得到多少條消息。這裏有2個,但最近可能會有所不同。 – Lisek
printf打印到標準輸出。標準輸出是行緩衝。後綴全部你想用printf用新行打印。它將被立即打印。 – alk