C++ socket programming in Linux

Server.c

#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/io.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define szSTR 256
#define SERVERPORT 21429 /*please modify the port with your id*/ int do_listen(const int port, const int bTcp)
{
int s = , r = , o = ;
struct sockaddr_in h;
memset(&h, , sizeof(h));
h.sin_family = AF_INET; h.sin_port = htons(port);
h.sin_addr.s_addr = INADDR_ANY;
s = socket(AF_INET, bTcp?SOCK_STREAM:SOCK_DGRAM, );
if (s < ) { perror("socket(listen)"); return ;}
r = setsockopt(s, SOL_SOCKET,SO_REUSEADDR, (char *)&o, sizeof(int));
if (r == -) { perror("setsockopt(listen)"); return ;}
r = bind(s, (struct sockaddr *)&h, sizeof(h));
if (r == -) { perror("bind(listen)"); return ;}
if (bTcp) {
r = listen(s, SOMAXCONN);
if (r == -) { perror("listen()"); return ;}
}/*end if*/
/*signal(SIGPIPE, SIG_IGN);*/
return s;
}/*end do_listen*/ void response(int sck, struct sockaddr_in * host)
{
FILE * f = ; char cmd[szSTR]; time_t now;
struct tm * t = ;
f = fdopen(sck, "w+");
while(!feof(f)) {
memset(cmd, , szSTR);
fgets(cmd, szSTR -, f);
time(&now);
t = localtime(&now);
if(strstr(cmd, "DATE")) {
fprintf(stderr, "Query Type: Date\n");
fprintf(f, "Date: %d %d, %d\n", t->tm_mon + , t->tm_mday, t->tm_year + );
continue;
}/*end if*/
if(strstr(cmd, "TIME")) {
fprintf(stderr, "Query Type: Time\n");
fprintf(f, "Time: %02d::%02d::%02d\n", t->tm_hour, t->tm_min, t->tm_sec);
continue;
}/*end if*/
if(strstr(cmd, "EXIT")) break;
fprintf(f, "commands: DATE, TIME, EXIT.\n");
}/*end while*/
shutdown(sck, SHUT_RDWR);
close(sck);
fclose(f);
return ;
}/*end response*/ int main(void)
{
socklen_t sklen = ;int sck = , i = , listener = ;
struct sockaddr_in client; pid_t proc = ;
system("ifconfig");
listener = do_listen(SERVERPORT, );
if(listener < ) { perror("listen()"); return ; }
for(i=;i<;i++) {
memset(&client, , sizeof(client));
sklen = sizeof(client);
sck = accept(listener, (struct sockaddr *)&client, &sklen);
if(sck < ) break;
proc = fork();
if (proc == -) { perror("fork()"); break ; }
if(proc) continue;
response(sck, &client);
break;
}/*next*/
close(listener);
return ;
}/*end main*/

Client.c

#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/io.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#define szSTR 256
#define SERVERPORT 21429 /*please modify the port with your id*/
int cnn(const char * ip, const int port)
{
struct sockaddr_in h; memset(&h, , sizeof(h));
h.sin_family = AF_INET; h.sin_port = htons(port);
h.sin_addr.s_addr = inet_addr(ip);
int s = socket(AF_INET, SOCK_STREAM, );
if (s < ) { perror("socket(tcp)"); return ;}
int r = connect(s, (struct sockaddr *)&h, sizeof(h));
if (r == ) return s;
perror("connect()");
return ;
}//end cnn int main(void)
{
int sck = ; FILE * f = ; char ip[szSTR]="127.0.0.1";
fprintf(stderr, "Please input the calendar server ip:");
fgets(ip, szSTR - , stdin);
sck = cnn(ip, SERVERPORT);
if(sck < ) return ;
f = fdopen(sck, "w+");
fprintf(f, "DATE\r\n");
fgets(ip, szSTR - , f);
fprintf(stderr, "%s\n", ip);
fprintf(f, "TIME\r\n");
fgets(ip, szSTR - , f);
fprintf(stderr, "%s\n", ip);
fprintf(f, "EXIT\r\n");
fclose(f);
close(sck);
return ;
}/*end main*/
上一篇:PHP重构之函数上移


下一篇:Java中执行外部命令