客户代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(const char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n,choice;
struct sockaddr_in serv_addr;
struct hostent *server;
int buffer;
if (argc < 3)
{
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
//printf("Please enter positive integer ");
printf("Enter your choice\n1=Prime number\n2=Fibonacci number\n 3=power of 2\n");
scanf("%d",&choice);
if(choice==1){
printf("Please enter positive integer ");
scanf("%d", &buffer);
}
n = write(sockfd,&buffer,sizeof(buffer));
if (n < 0)
error("ERROR writing to socket");
char msg[256];
bzero(msg, 256);
n=read(sockfd, msg, 255);
printf("%d %s\n",buffer, msg);
close(sockfd);
return 0;
}
服务器代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
int prime(int num)
{
int c;
for ( c = 2 ; c <= num - 1 ; c++ )
{
if ( num%c == 0 )
{
return 0;
}
}
if ( c == num )
return 1;
return 0;
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
int buffer;
struct sockaddr_in serv_addr, cli_addr;
int n;
int i=1;
if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
if (newsockfd < 0)
error("ERROR on accept");
n = read(newsockfd,&buffer,sizeof(buffer));
int result= prime(buffer);
if (n < 0) error("ERROR reading from socket");
printf("Client's input is: %d\n",buffer);
if(result==1)
{
n = write(newsockfd,"is Prime Number",18);
if (n < 0) error("ERROR writing to socket");
}
else
{
n = write(newsockfd,"is not Prime Number",18);
if (n < 0) error("ERROR writing to socket");
}
close(newsockfd);
close(sockfd);
return 0;
}
在此客户端 - 服务器进程中,服务器程序在首次执行后终止。我想:
答案 0 :(得分:2)
你显然需要循环和分叉。
答案 1 :(得分:0)
围绕accept
/ read
/ write
/ close
的无限循环会让您的服务器继续运行。
对于同时接受多个连接,您可以选择在accept
之后创建新进程或线程。另一种方法是使套接字无阻塞(Google是你的朋友!)并使用select
或poll
等轮询功能。
答案 2 :(得分:0)
请参阅我对此任务的实施: https://github.com/H2CO3/TCPHelper
基本技巧是你需要将accept()关闭到无限循环中,并在fork()之后或者在接受连接之后创建一个新线程。
如果你不想使用我的帮助函数,你会在这里看到一些非常好的教程: http://www.linuxhowtos.org/C_C++/socket.htm
答案 3 :(得分:0)
在服务器端
while (1){
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
if (newsockfd < 0)
error("ERROR on accept");
n = read(newsockfd,&buffer,sizeof(buffer));
int result= prime(buffer);
if (n < 0) error("ERROR reading from socket");
printf("Client's input is: %d\n",buffer);
if(result==1)
{
n = write(newsockfd,"is Prime Number",18);
if (n < 0) error("ERROR writing to socket");
}
else
{
n = write(newsockfd,"is not Prime Number",18);
if (n < 0) error("ERROR writing to socket");
}
}
在客户端:
//printf("Please enter positive integer ");
while (choice != -1){
printf("Enter your choice\n1=Prime number\n2=Fibonacci number\n 3=power of 2\n");
fflush(stdin);
scanf("%d",&choice);
if(choice==1){
printf("Please enter positive integer ");
scanf("%d", &buffer);
}
n = write(sockfd,&buffer,sizeof(buffer));
if (n < 0)
error("ERROR writing to socket");
char msg[256];
bzero(msg, 256);
n=read(sockfd, msg, 255);
printf("%d %s\n",buffer, msg);
}// end of while (choice != -1){
在服务器端,您需要创建一个流程,也许您可以实现流程同步系统。关键部分,锁定资源等。