diff --git a/cc.c b/cc.c deleted file mode 100644 index 172a7c9..0000000 --- a/cc.c +++ /dev/null @@ -1,74 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -int main(int argc, char *argv[]) -{ - int sockfd = 0, n = 0; - char recvBuff[1024]; - struct sockaddr_in serv_addr; - - if(argc != 2) - { - printf("\n Usage: %s \n",argv[0]); - return 1; - } - - memset(recvBuff, '0',sizeof(recvBuff)); - - /* a socket is created through call to socket() function */ - if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) - { - printf("\n Error : Could not create socket \n"); - return 1; - } - - memset(&serv_addr, '0', sizeof(serv_addr)); - - serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(5000); - - if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0) - { - printf("\n inet_pton error occured\n"); - return 1; - } - - /* Information like IP address of the remote host and its port is - * bundled up in a structure and a call to function connect() is made - * which tries to connect this socket with the socket (IP address and port) - * of the remote host - */ - if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) - { - printf("\n Error : Connect Failed \n"); - return 1; - } - - /* Once the sockets are connected, the server sends the data (date+time) - * on clients socket through clients socket descriptor and client can read it - * through normal read call on the its socket descriptor. - */ - while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0) - { - recvBuff[n] = 0; - if(fputs(recvBuff, stdout) == EOF) - { - printf("\n Error : Fputs error\n"); - } - } - - if(n < 0) - { - printf("\n Read error \n"); - } - - return 0; -} diff --git a/client.c b/client.c deleted file mode 100644 index 07003c7..0000000 --- a/client.c +++ /dev/null @@ -1,99 +0,0 @@ -#include "inclu.h" - - -/*readline函数实现*/ -ssize_t readline(int fd, char *vptr, size_t maxlen) -{ - ssize_t n, rc; - char c, *ptr; - - ptr = vptr; - for (n = 1; n < maxlen; n++) { - if ( (rc = read(fd, &c,1)) == 1) { - *ptr++ = c; - if (c == '\n') - break; /* newline is stored, like fgets() */ - } else if (rc == 0) { - *ptr = 0; - return(n - 1); /* EOF, n - 1 bytes were read */ - } else - return(-1); /* error, errno set by read() */ - } - - *ptr = 0; /* null terminate like fgets() */ - return(n); -} - - -int main(int argc , char ** argv) -{ - /*声明套接字和链接服务器地址*/ - int sockfd; - struct sockaddr_in servaddr; - - /*判断是否为合法输入*/ - if(argc != 2) - { - perror("usage:tcpcli "); - exit(1); - }//if - - /*(1) 创建套接字*/ - if((sockfd = socket(AF_INET , SOCK_STREAM , 0)) == -1) - { - perror("socket error"); - exit(1); - }//if - - /*(2) 设置链接服务器地址结构*/ - bzero(&servaddr , sizeof(servaddr)); - servaddr.sin_family = AF_INET; - servaddr.sin_port = htons(PORT); - if(inet_pton(AF_INET , argv[1] , &servaddr.sin_addr) < 0) - { - printf("inet_pton error for %s\n",argv[1]); - exit(1); - }//if - - /*(3) 发送链接服务器请求*/ - if( connect(sockfd , (struct sockaddr *)&servaddr , sizeof(servaddr)) < 0) - { - perror("connect error"); - exit(1); - }//if - - /*(4) 消息处理*/ - char sendline[MAX_LINE] , recvline[MAX_LINE]; - while(fgets(sendline , MAX_LINE , stdin) != NULL) - { - write(sockfd , sendline , strlen(sendline)); - - if(readline(sockfd , recvline , MAX_LINE) == 0) - { - perror("server terminated prematurely"); - exit(1); - }//if - - if(fputs(recvline , stdout) == EOF) - { - perror("fputs error"); - exit(1); - }//if - - if(readline(sockfd , recvline , MAX_LINE) == 0) - { - perror("server terminated prematurely"); - exit(1); - }//if - - if(fputs(recvline , stdout) == EOF) - { - perror("fputs error"); - exit(1); - }//if - - }//while - - /*(5) 关闭套接字*/ - close(sockfd); -} diff --git a/client/def.h b/client/def.h new file mode 100644 index 0000000..4729c47 --- /dev/null +++ b/client/def.h @@ -0,0 +1,9 @@ +int sockfd; +int sockad; +char s[3]; +struct sockaddr_in seraddr; +struct sockaddr_in cliaddr; +int len; + + +void init_con(); diff --git a/client/func/init_con.c b/client/func/init_con.c new file mode 100644 index 0000000..61c4be1 --- /dev/null +++ b/client/func/init_con.c @@ -0,0 +1,9 @@ +void init_con(){ + sockad = socket(AF_INET,SOCK_STREAM,0); + memset(&seraddr,0,sizeof(struct sockaddr_in)); + seraddr.sin_family = AF_INET; + seraddr.sin_port = htons(8000); + seraddr.sin_addr.s_addr = inet_addr("127.0.0.1"); + connect(sockad,(struct sockaddr *)&seraddr, sizeof(struct sockaddr)); + +} diff --git a/inclu.h b/client/inclu.h similarity index 53% rename from inclu.h rename to client/inclu.h index 0fd133b..d14c918 100644 --- a/inclu.h +++ b/client/inclu.h @@ -1,5 +1,11 @@ -#include "def.h" #include #include #include #include +#include +#include +#include + +#include "def.h" + +#include "func/init_con.c" diff --git a/client/main.c b/client/main.c new file mode 100644 index 0000000..358410b --- /dev/null +++ b/client/main.c @@ -0,0 +1,14 @@ +#include "inclu.h" + +int main(int argc, char *argv[]) +{ + + init_con(); + len=recv(sockad,s,1024,0); +// s[len]='\0'; + printf("%s\n",s); + close(sockad); + + + return EXIT_SUCCESS; +} diff --git a/cli.py b/python/cli.py similarity index 100% rename from cli.py rename to python/cli.py diff --git a/ser.py b/python/ser.py similarity index 100% rename from ser.py rename to python/ser.py diff --git a/s.c b/s.c deleted file mode 100644 index 967d00e..0000000 --- a/s.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "inclu.h" - -int main(int argc,char **argv) -{ - sockfd = socket(AF_INET,SOCK_STREAM,0); - bzero(&seraddr,sizeof(seraddr)); - seraddr.sin_family = AF_INET; - seraddr.sin_port = htons(800); - seraddr.sin_addr.s_addr = inet_addr("127.0.0.1"); - bind(sockfd,(struct sockaddr *)&seraddr,sizeof(struct sockaddr)); - listen(sockfd, 5); - accept(sockad, NULL, NULL); - send(sockad,s , sizeof(char), 0); - //recv(sockad, const void *buf, size_t nbytes, int flags); - - return 0; - -} diff --git a/server.c b/server.c deleted file mode 100644 index 54859d9..0000000 --- a/server.c +++ /dev/null @@ -1,76 +0,0 @@ -#include "inclu.h" - -int main(int argc , char **argv) -{ - char second[]="second"; - /*声明服务器地址和客户链接地址*/ - struct sockaddr_in servaddr , cliaddr; - - /*声明服务器监听套接字和客户端链接套接字*/ - int listenfd , connfd; - pid_t childpid; - - /*声明缓冲区*/ - char buf[MAX_LINE]; - - socklen_t clilen; - - /*(1) 初始化监听套接字listenfd*/ - if((listenfd = socket(AF_INET , SOCK_STREAM , 0)) < 0) - { - perror("socket error"); - exit(1); - }//if - - /*(2) 设置服务器sockaddr_in结构*/ - bzero(&servaddr , sizeof(servaddr)); - - servaddr.sin_family = AF_INET; - servaddr.sin_addr.s_addr = htonl(INADDR_ANY); //表明可接受任意IP地址 - servaddr.sin_port = htons(PORT); - - /*(3) 绑定套接字和端口*/ - if(bind(listenfd , (struct sockaddr*)&servaddr , sizeof(servaddr)) < 0) - { - perror("bind error"); - exit(1); - }//if - - /*(4) 监听客户请求*/ - if(listen(listenfd , LISTENQ) < 0) - { - perror("listen error"); - exit(1); - }//if - - /*(5) 接受客户请求*/ - for( ; ; ) - { - clilen = sizeof(cliaddr); - if((connfd = accept(listenfd , (struct sockaddr *)&cliaddr , &clilen)) < 0 ) - { - perror("accept error"); - exit(1); - }//if - - //新建子进程单独处理链接 - if((childpid = fork()) == 0) - { - close(listenfd); - //str_echo - ssize_t n; - char buff[MAX_LINE]; - while((n = read(connfd , buff , MAX_LINE)) > 0) - { - write(connfd , buff , n); - write(connfd , second , 6); - - } - exit(0); - }//if - close(connfd); - }//for - - /*(6) 关闭监听套接字*/ - close(listenfd); -} diff --git a/def.h b/server/def.h similarity index 67% rename from def.h rename to server/def.h index 1a2f57c..0233ff5 100644 --- a/def.h +++ b/server/def.h @@ -1,5 +1,8 @@ int sockfd; int sockad; -char *s; +char s[3]; struct sockaddr_in seraddr; struct sockaddr_in cliaddr; +int len; + +void init_con(); diff --git a/server/func/init_con.c b/server/func/init_con.c new file mode 100644 index 0000000..0fc2ca4 --- /dev/null +++ b/server/func/init_con.c @@ -0,0 +1,3 @@ +void init_con(){ + +} diff --git a/server/inclu.h b/server/inclu.h new file mode 100644 index 0000000..6be1397 --- /dev/null +++ b/server/inclu.h @@ -0,0 +1,12 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "def.h" + + +#include "func/init_con.c" diff --git a/server/main.c b/server/main.c new file mode 100644 index 0000000..888b956 --- /dev/null +++ b/server/main.c @@ -0,0 +1,27 @@ +#include "inclu.h" + +int main(int argc,char **argv) +{ + len=3; + int sin_size; + sin_size=sizeof(struct sockaddr_in); + s[0]='3'; + s[1]='4'; + s[2]='1'; + sockfd = socket(AF_INET,SOCK_STREAM,0); + memset(&seraddr,0,sizeof(struct sockaddr_in)); + seraddr.sin_family = AF_INET; + seraddr.sin_port = htons(8000); + seraddr.sin_addr.s_addr = inet_addr("127.0.0.1"); + bind(sockfd,(struct sockaddr *)&seraddr,sizeof(struct sockaddr)); + listen(sockfd, 5); + printf("start listen\n"); + sockad=accept(sockfd, (struct sockaddr *)&cliaddr, &sin_size); + send(sockad,s, len, 0); + close(sockfd); + close(sockad); + //recv(sockad, const void *buf, size_t nbytes, int flags); + + return 0; + +} diff --git a/ss.c b/ss.c deleted file mode 100644 index 186dd61..0000000 --- a/ss.c +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -int main(int argc, char *argv[]) -{ - int listenfd = 0, connfd = 0; - struct sockaddr_in serv_addr; - - char sendBuff[1025]; - time_t ticks; - - /* creates an UN-named socket inside the kernel and returns - * an integer known as socket descriptor - * This function takes domain/family as its first argument. - * For Internet family of IPv4 addresses we use AF_INET - */ - listenfd = socket(AF_INET, SOCK_STREAM, 0); - memset(&serv_addr, '0', sizeof(serv_addr)); - memset(sendBuff, '0', sizeof(sendBuff)); - - serv_addr.sin_family = AF_INET; - serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); - serv_addr.sin_port = htons(5000); - - /* The call to the function "bind()" assigns the details specified - * in the structure 『serv_addr' to the socket created in the step above - */ - bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); - - /* The call to the function "listen()" with second argument as 10 specifies - * maximum number of client connections that server will queue for this listening - * socket. - */ - listen(listenfd, 10); - - while(1) - { - /* In the call to accept(), the server is put to sleep and when for an incoming - * client request, the three way TCP handshake* is complete, the function accept() - * wakes up and returns the socket descriptor representing the client socket. - */ - connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); - - /* As soon as server gets a request from client, it prepares the date and time and - * writes on the client socket through the descriptor returned by accept() - */ - ticks = time(NULL); - snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks)); - write(connfd, sendBuff, strlen(sendBuff)); - - close(connfd); - sleep(1); - } -} - diff --git a/sss.c b/sss.c deleted file mode 100644 index 15f489a..0000000 --- a/sss.c +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -int main(int argc, const char * argv[]) { - // insert code here... - printf("Hello, World!\n"); - - int server_sockfd; //服务端套接字 - int client_sockfd; //客户端套接字 - int len; - struct sockaddr_in my_add; //服务器网络地址结构体 - struct sockaddr_in remote_addr; //客户端网络地址结构体 - socklen_t sin_size; - char buf[BUFSIZ]; //数据传送的缓冲区 - memset(&my_add,0,sizeof(my_add)); //数据初始化--清零 - my_add.sin_family = AF_INET; //设置为IP通信 - my_add.sin_addr.s_addr = INADDR_ANY; //服务器IP地址--允许连接到所有本地地址上 - my_add.sin_port = htons(8000); //服务器端口号 - - //创建服务器端套接字--IPV4协议 ,面向连接通信,TCP协议 - if((server_sockfd=socket(PF_INET, SOCK_STREAM, 0))<0) { - perror("socket"); - return 1; - } - - //将套接字绑定到服务器的网络地址上 - if (bind(server_sockfd, (struct sockaddr *)&my_add, sizeof(struct sockaddr))<0) { - perror("bind"); - return 1; - } - - //监听连接请求--监听队列长度为5 - listen(server_sockfd, 5); - sin_size = sizeof(struct sockaddr_in); - - //等待客户端连接请求到达 - if ((client_sockfd=accept(server_sockfd, (struct sockaddr *)&remote_addr, &sin_size))<0) { - perror("accept"); - return 1; - } - - printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr)); - len = send(client_sockfd, "welcome to my server\n", 21, 0); //发送欢迎信息 - - //接受客户端的数据并将其发送给客户端--recv返回接收到的字节数,send返回发送的字节数 - while (1) { - memset(buf,0,sizeof(buf)); - len = recv(client_sockfd, buf, BUFSIZ, 0); - send(client_sockfd,buf, len, 0); - buf[len]='\0'; - printf("receive:%s\n",buf); - if(buf[0]=='q'){ - send(client_sockfd,'q', 1, 0); - break; - } - } - - close(client_sockfd); - close(server_sockfd); - - return 0; -} - diff --git a/ccc.c b/test/cli.c similarity index 98% rename from ccc.c rename to test/cli.c index afd1310..afc7c8d 100644 --- a/ccc.c +++ b/test/cli.c @@ -28,13 +28,12 @@ int main(int argc, const char * argv[]) { client_sockfd=socket(PF_INET, SOCK_STREAM, 0); - connect(client_sockfd, (struct sockaddr *)&remota_addr, sizeof(struct sockaddr)); printf("connect to server\n"); len = recv(client_sockfd, buf, BUFSIZ, 0); - buf[len]='/0'; + buf[len]='\0'; printf("%s",buf); diff --git a/test/ser.c b/test/ser.c new file mode 100644 index 0000000..7071a82 --- /dev/null +++ b/test/ser.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, const char * argv[]) { + char quit='q'; + int server_sockfd; + int client_sockfd; + int len; + struct sockaddr_in my_add; + struct sockaddr_in remote_addr; + socklen_t sin_size; + char buf[BUFSIZ]; + memset(&my_add,0,sizeof(my_add)); + my_add.sin_family = AF_INET; + my_add.sin_addr.s_addr = INADDR_ANY; + my_add.sin_port = htons(8000); + + server_sockfd=socket(PF_INET, SOCK_STREAM, 0); + + bind(server_sockfd, (struct sockaddr *)&my_add, sizeof(struct sockaddr)); + + listen(server_sockfd, 5); + sin_size = sizeof(struct sockaddr_in); + + client_sockfd=accept(server_sockfd, (struct sockaddr *)&remote_addr, &sin_size); + + printf("accept client %s\n",inet_ntoa(remote_addr.sin_addr)); + len = send(client_sockfd, "welcome to my server\n", 21, 0); + + while (1) { + memset(buf,0,sizeof(buf)); + len = recv(client_sockfd, buf, BUFSIZ, 0); + send(client_sockfd,buf, len, 0); + buf[len]='\0'; + printf("receive:%s\n",buf); + if(buf[0]=='q'){ + send(client_sockfd,&quit, 1, 0); + break; + } + } + close(client_sockfd); + close(server_sockfd); + return 0; +} +