30/09/2018, 18:18
giúp đỡ về foking server trong linux
mình có làm 1 chương trình mạng có nhiều client chạy chung 1 server có áp dụng fork theo hướng dẫn trên mạng, nhưng nó chỉ chạy cho 1 client thôi, client khác ko chạy đc
code server:
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
#inlude<signal.h>
void touppers(char*s){
int i;
for (i=0;i 0)
printf("child %d terminated
", pid);
return;
}
int main(int argc, char const *argv[])
{
int socket_desc, client_sock, c, read_size;
struct sockaddr_in server, client;
char client_message[2000];
//create SOcket
socket_desc = socket(AF_INET,SOCK_STREAM,0);
if(socket_desc == -1){
printf("could not create socket
");
}
puts("socket create");
// set the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(5500);
//bind
if(bind(socket_desc,(struct sockaddr*)&server,sizeof(server))<0){
perror("bind failed.Error");
return 1;
}
puts("Bind done!");
//listen
listen(socket_desc,3);
void doit(int client_sock,char *client_message){
int read_size;
while(read_size = recv(client_sock,client_message,2000,0)>0){
touppers(client_message);
write(client_sock,client_message,strlen(client_message));
}
}
//accept connection from client
puts("Waiting for incoming client");
c = sizeof(struct sockaddr);
pid_t pid;
signal(SIGCHLD,sig_chld);
// loop
for( ; ; ){
client_sock = accept(socket_desc,(struct sockaddr *)&client,(socklen_t)&c);
if((pid = fork() ==0)){
close(socket_desc);
doit(client_sock,client_message);
exit(0);
}
close(client_sock);
}
}
còn đây là client:
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
int main(int argc , char *argv[])
{
int sock,sent_size,sum;
sum =0;
char c;
struct sockaddr_in server;
char sendbuff[1000] , recvbuff[2000];
//Create socket
sock = socket(AF_INET , SOCK_STREAM , 0);
if (sock == -1)
{
printf("[Client]: Could not create socket");
}
puts("[Client]: Socket created");
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons(5500);
//Connect to remote server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("[Client]: Connect failed. Error");
return 1;
}
puts("[Client]: Connected
");
//keep communicating with server
while(1)
{
printf("[Client]: Enter message : ");
fgets(sendbuff,100,stdin);
if(strlen(sendbuff)==2){
sendbuff[strlen(sendbuff)-1] = ' ';
}
if((strcmp(sendbuff,"Q")==0)||(strcmp(sendbuff,"q")==0))
break;
//Send data
if( (sent_size=send(sock , sendbuff , strlen(sendbuff) , 0)) < 0)
{
puts("[Client]: Send failed");
return 1;
}
sum = sum +sent_size;
//Receive a reply from the server
if( recv(sock , recvbuff , 2000 , 0) < 0)
{
puts("[Client]: Recv failed");
break;
}
puts("Server reply :");
puts(recvbuff);
bzero(recvbuff,strlen(recvbuff));
}
printf("[Client]: Count sent byte : %d bytes
",sum);
close(sock);
return 0;
}
Bài liên quan
do mình post code toàn bị lỗi, code dài toàn copy, nên gửi link cho các bạn dễ nhìn
client http://codepad.org/G4FLkcV4
server http://codepad.org/PAAC88Wv
Bạn edit lại dùng markdown đi
client.c
còn đây là server