1// server 2#include <stdio.h> 3#include <unistd.h> 4#include <stdlib.h> 5#include <string.h> 6#include <malloc.h> 7#include <pthread.h> 8#include <semaphore.h> 9#include <sys/types.h> 10#include <sys/socket.h> 11#include <sys/time.h> 12#include <sys/ioctl.h> 13#include <netinet/in.h> 14#include <errno.h> 15#include <string.h> 16#include"time.h" 17 18 19#define SERVER_PORT 10000 20#define MAX_BUF_SIZE 16 21#define SUCCESS 0 22#define FAILURE -1 23 24int RecvFromServer(int iClientSockFd); 25 26int main(int argc, char **argv) 27{ 28 int iServerSockFd; 29 int iClientSockFd; 30 struct sockaddr_in stServerAddr; 31 struct sockaddr_in stClientAddr; 32 int iServerLen; 33 int iClientLen; 34 int iOpt =1; 35 int iReturn; 36 pid_t pid; 37 38 // time 39 time_t timer; 40 struct tm *tblock; 41 timer = time(NULL); 42 tblock= localtime(&timer); 43 printf("servertime is %s",asctime(tblock)); 44 45 printf("This is the Server.\n"); 46 printf("I am waiting the client to rsync time \n"); 47 48 // create server socket 49 iServerSockFd = socket(AF_INET, SOCK_STREAM, 0); 50 if (iServerSockFd < 0) 51 { 52 printf("socket create failed in CommManageServerRecvThread.\n"); 53 return FAILURE; 54 } 55 stServerAddr.sin_family = AF_INET; 56 stServerAddr.sin_addr.s_addr = htonl(INADDR_ANY); 57 stServerAddr.sin_port = htons(SERVER_PORT); 58 iServerLen = sizeof(stServerAddr); 59 60 // bind socket 61 iReturn = bind(iServerSockFd, (struct sockaddr *)&stServerAddr, iServerLen); 62 if (iReturn < 0) 63 { 64 printf("socket bind failed in CommManageServerRecvThread.\n"); 65 66 } 67 68 setsockopt(iServerSockFd,SOL_SOCKET,SO_REUSEADDR,&iOpt,sizeof(iOpt)); 69 70 // listen socket 71 iReturn = listen(iServerSockFd, 10); 72 73 // accept request from client 74 while(1) 75 { 76 iClientSockFd = accept(iServerSockFd, (struct sockaddr*)&stClientAddr, (unsigned *)&iClientLen); 77 if (iServerSockFd < 0) 78 { 79 printf("accept failed.\n"); 80 81 } 82 pid = fork(); 83 switch(pid) 84 { 85 case -1: 86 { 87 printf("fork failed.\n"); 88 break; 89 } 90 case 0: 91 { 92 write(iClientSockFd,asctime(tblock),30); 93 printf("child service ok!!\n"); 94 close(iClientSockFd); 95 break; 96 } 97 default: 98 { 99 close(iClientSockFd); 100 break; 101 } 102 } 103 104 } 105 106 close(iClientSockFd); 107 108 close(iServerSockFd); 109 110 return SUCCESS; 111} 112 113int RecvFromServer(int iClientSockFd) 114{ 115 char acBuf[MAX_BUF_SIZE]; 116 int iReadNum; 117 118 memset(acBuf, 0, sizeof(acBuf)); 119 120 iReadNum = recv(iClientSockFd, acBuf, 0xFFFF, 0); 121 if(iReadNum < 0) 122 { 123 printf("read failed.\n"); 124 return FAILURE; 125 } 126 127 printf("Receive form client:\n"); 128 printf("%s\n", acBuf); 129 130 return SUCCESS; 131} 132 133 134 135 136 137// client 138#include <stdio.h> 139#include <unistd.h> 140#include <stdlib.h> 141#include <string.h> 142#include <malloc.h> 143#include <pthread.h> 144#include <semaphore.h> 145#include <sys/types.h> 146#include <sys/socket.h> 147#include <sys/time.h> 148#include <sys/ioctl.h> 149#include <netinet/in.h> 150#include <errno.h> 151#include <string.h> 152 153 154#define SERVER_PORT 10000 155#define SERVER_IP "192.168.0.112" 156#define WRITE_BUF_SIZE 1024 157 158#define SUCCESS 0 159#define FAILURE -1 160char acBuf[WRITE_BUF_SIZE]; 161 162int my_read(int fd,void *buffer,int length) 163{ 164 int bytes_left; 165 int bytes_read; 166 char *ptr; 167 bytes_left=length; 168 169 ptr=buffer; 170 171 while(bytes_left>0) 172 { 173 bytes_read=read(fd,ptr,bytes_left); 174 if(bytes_read<0) 175 { 176 if(errno==EINTR) 177 bytes_read=0; 178 else 179 return -1; 180 } 181 else 182 if(bytes_read==0) 183 break; 184 bytes_left -= bytes_read; 185 ptr+=bytes_read; 186 } 187 return(length-bytes_left); 188} 189 190int main(int argc, char **argv) 191{ 192 int iSockFd; 193 int iReadNum; 194 195 struct sockaddr_in stServerAddr; 196 struct sockaddr_in stClientAddr; 197 int iServerLen; 198 int iClientLen; 199 int iReturn; 200 201 202 printf("This is the client.\n"); 203 204 205 // create server socket 206 iSockFd = socket(AF_INET, SOCK_STREAM, 0); 207 if (iSockFd < 0) 208 { 209 printf("socket create failed in CommManageServerRecvThread.\n"); 210 return FAILURE; 211 } 212 stServerAddr.sin_family = AF_INET; 213 inet_pton(AF_INET, SERVER_IP, &stServerAddr.sin_addr); 214 stServerAddr.sin_port = htons(SERVER_PORT); 215 216 iServerLen = sizeof(stServerAddr); 217 218 while(1) 219 { 220 //printf("try to connect to the server... ...\n"); 221 iReturn = connect(iSockFd, (const struct sockaddr *)&stServerAddr, iServerLen); 222 if(iReturn >= 0) 223 { 224 break; 225 } 226 227 sleep(3); 228 } 229 230 printf("connect SUCCESS.\n"); 231 232 memset(acBuf, 0, sizeof(acBuf)); 233 234 my_read(iSockFd, acBuf, 30); 235 236 printf("%s\n",acBuf); 237 238 close(iSockFd); 239 240 return SUCCESS; 241}
Linux C基于进程并发的服务器简单示例
Stella981
2021-10-11
1105 0 0
点赞
收藏
评论区
加载中...