#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void doprocessing (int sock)
{
int n,i_recvBytes;
char buffer[256];
//bzero(buffer,256);
while(1){
printf("waiting for request...\n");
//Reset data.
memset(buffer,0,256);
i_recvBytes = read(sock,buffer,255);
if(i_recvBytes == 0){
printf("Maybe the client has closed\n");
break;
}
if(i_recvBytes == -1){
fprintf(stderr,"read error!\n");
break;
}
if(strcmp(buffer,"quit")==0){
printf("Quit command!\n");
break; //Break the while loop.
}
printf("read from client : %s\n",buffer);
if(write(sock,buffer,strlen(buffer)) == -1){
break;
}
}
}
int main( int argc, char *argv[] )
{
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int pid;
int n;
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
exit(1);
}
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5001;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
{
perror("ERROR on binding");
exit(1);
}
/* Now start listening for the clients, here
* process will go in sleep mode and will wait
* for the incoming connection
*/
listen(sockfd,5);
clilen = sizeof(cli_addr);
while (1)
{
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
perror("ERROR on accept");
exit(1);
}
/* Create child process */
pid = fork();
if (pid < 0)
{
perror("ERROR on fork");
exit(1);
}
if (pid == 0)
{
/* This is the client process */
close(sockfd);
doprocessing(newsockfd);
exit(0);
}
else
{
close(newsockfd);//shutdown(newsockfd);或许会更好
}
} /* end of while */
}
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void doprocessing (int sock)
{
int n,i_recvBytes;
char buffer[256];
//bzero(buffer,256);
while(1){
printf("waiting for request...\n");
//Reset data.
memset(buffer,0,256);
i_recvBytes = read(sock,buffer,255);
if(i_recvBytes == 0){
printf("Maybe the client has closed\n");
break;
}
if(i_recvBytes == -1){
fprintf(stderr,"read error!\n");
break;
}
if(strcmp(buffer,"quit")==0){
printf("Quit command!\n");
break; //Break the while loop.
}
printf("read from client : %s\n",buffer);
if(write(sock,buffer,strlen(buffer)) == -1){
break;
}
}
}
int main( int argc, char *argv[] )
{
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int pid;
int n;
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
perror("ERROR opening socket");
exit(1);
}
/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5001;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
/* Now bind the host address using bind() call.*/
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
{
perror("ERROR on binding");
exit(1);
}
/* Now start listening for the clients, here
* process will go in sleep mode and will wait
* for the incoming connection
*/
listen(sockfd,5);
clilen = sizeof(cli_addr);
while (1)
{
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
{
perror("ERROR on accept");
exit(1);
}
/* Create child process */
pid = fork();
if (pid < 0)
{
perror("ERROR on fork");
exit(1);
}
if (pid == 0)
{
/* This is the client process */
close(sockfd);
doprocessing(newsockfd);
exit(0);
}
else
{
close(newsockfd);//shutdown(newsockfd);或许会更好
}
} /* end of while */
}