ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Web服务器1-socket编程

2021-12-11 09:33:13  阅读:201  来源: 互联网

标签:Web MAXLINE socket int 编程 char rio tm include


基于华为鲲鹏云服务器CentOS中(或Ubuntu),使用Linux Socket实现:

1. time服务器的客户端服务器,提交程序运行截图

查看IP地址

代码如下:

//timeclient.c
#include <stdio.h>
#include <netinet/in.h>                                                                   
#include <arpa/inet.h>
#include <netdb.h>    
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>

#define RIO_BUFSIZE 8192

typedef struct{
    int rio_fd;
    int rio_cnt;
    char *rio_bufptr;
    char rio_buf[RIO_BUFSIZE];

}rio_t;

#define MAXLINE 200

int main(int argc,char **argv)
{
    int clientfd,port;
    char *host,buf[MAXLINE];
    char sbuf[MAXLINE];
    char rbuf[MAXLINE];
    rio_t rio;
    char str1[MAXLINE]="客户端IP:";
    char str2[MAXLINE]="学号:20191316";
    char str3[MAXLINE]="当地时间:";
    if(argc!=3)
    {
        fprintf(stderr,"usage:%s <host> <port>\n",argv[0]);
        exit(0);
    }
    host = argv[1];
    port = atoi(argv[2]);
    clientfd = open_clientfd(host,port);  
    while(1)
    {        
        recv(clientfd,rbuf,MAXLINE,0);
        printf("%s",str1);
        puts(host);
        printf("%s",str2);
        putchar('\n');
        printf("%s",str3);     
        puts(rbuf);       
        close(clientfd);      
        exit(0);
    }
}

//timeserver.c
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>                                                                   
#include <arpa/inet.h>
#include <netdb.h>    
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define MAXLINE 200
#define RIO_BUFSIZE 8192

typedef struct{
	   int rio_fd;
	   int rio_cnt;
	   char *rio_bufptr;
	   char rio_buf[RIO_BUFSIZE];

}rio_t;

typedef struct sockaddr SA;

typedef struct{
	int tm_sec;
	int tm_min;
	int tm_hour;
	int tm_mday;
	int tm_mon;
	int tm_year;
	int tm_wday;
	int tm_yday;
	int tm_isdst;
}tm;

void sigchld_handler(int sig)
{
	    pid_t pid;
	    int stat;
	    while((pid = waitpid(-1,&stat,WNOHANG))>0)
            {
	        printf("child %d terminated\n",pid); 
            }
	    return;
}

int main(int argc,char **argv)
{
	    int listenfd,connfd,port,clientlen;
	    struct sockaddr_in clientaddr;
	    struct hostent *hp;
	    char *haddrp;
	    char sbuf[MAXLINE];
	    char rbuf[MAXLINE];
	    rio_t rio;
	    time_t lt;
	    tm *local;
	    char str1[MAXLINE]="客户端IP:";
	    char str2[MAXLINE]="服务器实现者学号:";
	    char str3[MAXLINE]="当地时间:";
            if(argc != 2)
            {							        
                fprintf(stderr,"usage:%s <port>\n",argv[0]);
                  exit(0);
	    }
            port = atoi(argv[1]);
            signal(SIGCHLD,sigchld_handler);
            listenfd = open_listenfd(port)
            while(1)
            {							        
                 clientlen = sizeof(clientaddr);
                 connfd = accept(listenfd,(SA *)&clientaddr,&clientlen);
                 hp = gethostbyaddr((const char*)&clientaddr.sin_addr.s_addr,
                 sizeof(clientaddr.sin_addr.s_addr),AF_INET);
                 haddrp = inet_ntoa(clientaddr.sin_addr);
                 printf("server connected to %s (%s)\n",hp->h_name,haddrp);
                 if(fork() == 0)
                 {
                     close(listenfd);
                     lt = time(NULL); 
                     local = localtime(&lt);
                     strftime(sbuf,64,"%Y-%m-%d %H:%M:%S",local);
                     send(connfd,sbuf,MAXLINE,0);
                     close(connfd);
                     exit(0);
                 }
             close(connfd);
	     }																        }
}

2. echo服务器的客户端服务器,提交程序运行截图,服务器把客户端传进来的内容加入“服务器进程pid 你的学号 姓名 echo :”返回给客户端

可见客户端输入的内容被服务器又返回给客户端了

代码如下

\\echoclient.c
#include <stdio.h>
#include <netinet/in.h>                                                                   
#include <arpa/inet.h>
#include <netdb.h>    
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>

#define RIO_BUFSIZE 8192

typedef struct{
    int rio_fd;
    int rio_cnt;
    char *rio_bufptr;
    char rio_buf[RIO_BUFSIZE];

}rio_t;

#define MAXLINE 200

int main(int argc,char **argv){

    int clientfd,port;
    char *host,buf[MAXLINE];
    char sbuf[MAXLINE];
    char rbuf[MAXLINE];
    rio_t rio;
    char str1[MAXLINE]="服务器进程pid:";
    char str2[MAXLINE]="学号姓名:20191316 王秋雨";
    
    char str3[MAXLINE]="echo:";

    if(argc!=3){
    
        fprintf(stderr,"usage:%s <host> <port>\n",argv[0]);
        exit(0);
    }
    host = argv[1];
    port = atoi(argv[2]);

    clientfd = open_clientfd(host,port);
  
    while(1){
        scanf("%[^\n]",sbuf);
        send(clientfd,sbuf,MAXLINE,0);

        recv(clientfd,rbuf,MAXLINE,0);

        printf("%s",str1);
        printf("%d\n",getpid());

        printf("%s",str2);
        putchar('\n');

        printf("%s",str3);

        puts(rbuf);
       
        close(clientfd);
       
        exit(0);
    }

}

//echoserver.c
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>                                                                   
#include <arpa/inet.h>
#include <netdb.h>    
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <pthread.h>

#define MAXLINE 200
#define RIO_BUFSIZE 8192

typedef struct{
    int rio_fd;
    int rio_cnt;
    char *rio_bufptr;
    char rio_buf[RIO_BUFSIZE];

}rio_t;

typedef struct sockaddr SA;

typedef struct{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}tm;


void *thread(void *vargp);

int main(int argc,char **argv){

    int listenfd,*connfdp,port;
    int clientlen;
    struct sockaddr_in clientaddr;
    struct hostent *hp;
    char *haddrp;
    pthread_t tid;

    if(argc != 2){
    
        fprintf(stderr,"usage:%s <port>\n",argv[0]);
        exit(0);
    }

    port = atoi(argv[1]);

    listenfd = open_listenfd(port);
    
    
    while(1){
    
        clientlen = sizeof(clientaddr);

        connfdp =malloc(sizeof(int));
    
        *connfdp = accept(listenfd,(SA *)&clientaddr,&clientlen);


        hp = gethostbyaddr((const char*)&clientaddr.sin_addr.s_addr,
                sizeof(clientaddr.sin_addr.s_addr),AF_INET);

        haddrp = inet_ntoa(clientaddr.sin_addr);

        printf("server connected to %s (%s)\n",hp->h_name,haddrp);

        pthread_create(&tid,NULL,thread,connfdp);

        pthread_join(tid,NULL);
    }
}


void *thread(void *vargp){
    
    time_t lt;
    tm *local;
    char sbuf[MAXLINE];
    char rbuf[MAXLINE];
    int connfd = *((int*)vargp);

    free(vargp);

    pthread_detach(pthread_self());

    recv(connfd,rbuf,MAXLINE,0);
    printf("The massage is :%s\n",rbuf);
    send(connfd,rbuf,MAXLINE,0);

    close(connfd);
    
    return NULL;
}

全部代码如下:https://gitee.com/qiu_yu_wang/code/tree/master/socket

3. 服务器部署到华为云服务器,客户端用Ubuntu虚拟机。

由于华为云余额不足,我选择了两个虚拟机来操作

查看虚拟机的IP地址为:192.168.78.128

标签:Web,MAXLINE,socket,int,编程,char,rio,tm,include
来源: https://www.cnblogs.com/ffffatal/p/15633207.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有