gpt4 book ai didi

c中socket编程的客户端服务器进程执行

转载 作者:行者123 更新时间:2023-11-30 14:26:33 25 4
gpt4 key购买 nike

客户端代码:

#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;
}

在这个客户端-服务器进程中,服务器程序在第一次执行后终止。我想要:

  1. 服务器将无限运行
  2. 如果我将客户端程序保存在多台电脑上,每个客户端都能够通信,并且服务器程序不会被终止。

最佳答案

那么你显然需要循环和 fork 。

关于c中socket编程的客户端服务器进程执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8971003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com