LINUX网络通信

字节序

字节序是指多字节数据在计算机内存中存储或者网络传输时各字节的存储顺序。

常见序

1 Little endian:将低序字节存储在起始地址 (小端字节序)
2 Big endian:将高序字节存储在起始地址 (大端字节序)
网诺字节序=大端字节序

socket服务器和客户端的开发步骤

服务器

1 创建套接字 socket()
2 为套接字添加信息(IP地址和端口号)bind()
3 监听网诺连接 listen()
4 监听到有客户端接入,接受一个连接 accept()
5 数据交互 read() write()
6 关闭套接字,断开连接 close()

客户端

1 创建套接字 socket()
2 连接服务器 cinnect()
3 数据交换 write() read()
4 关闭客户端 close()
socket函数
bind函数
listen函数
accept函数
connet函数

地址转换API

int inet_aton(const char* straddr, struct in_addr addrp);把字符串形式的“192.168.1.123”转为网络能识别的格式
char
inet_ntoa(struct in_addr inaddr); 把网络格式的IP地址转为字符串形式

字节序转换API

#include <netinet/in.h>
uint16_t htons(uint16_t host16bitvalue);//返回网络字节序的值
uint16_t htons(uint32_t host32bitvalue);//返回网络字节序的值
uint16_t ntohs(uint16_t net16bitvalue);//返回主机字节序的值
uint32_tntohl(uint32_t net32bitvalue);//返回主机字节序的值
h代表host,n代表net,s代表short(两个字节),l代表long(四个字节),通过上面的4个函数可以实现主机字节序和网络字节序之间的转换。有时`可以用INADDR_ANY, INADDR_ANY指定地址让操作系统自己获取

FTP服务器

 1 #include <stdio.h>
  2 #include <sys/types.h>          /* See NOTES */
  3 #include <sys/socket.h>
  4 //#include <linux/in.h>
  5 #include <arpa/inet.h>
  6 #include <netinet/in.h>
  7 #include <stdlib.h>
  8 #include <string.h>
  9 //#include <config.h>
 10 #include <sys/stat.h>
 11 #include <fcntl.h>
 12 
 13 #define LS 0
 14 #define GET 1
 15 #define PWD 2
 16 #define IFGO 3
 17 #define LCD 4
 18 #define LLS 5
 19 #define CD 6
 20 #define PUT 7
 21 #define QUIT 8
 22 #define DOFILE 9
 23 
 24 struct Msg
 25 {
 26         int type;
 27         char data[1024];
 28         char secondBuf[128];
 29 };
 30 
 31 int get_cmd_type(char *cmd)
 32 {
 33         if(!strcmp("ls",cmd))   return LS;
 34         if(!strcmp("quit",cmd))   return QUIT;
 35         if(!strcmp("pwd",cmd))   return PWD;
 36         if(strstr(cmd,"cd")!=NULL)   return CD;
 37         if(strstr(cmd,"get")!=NULL)   return GET;
 38         if(strstr(cmd,"put")!=NULL)   return PUT;
 39 
 40         return 100;
 41 }
 42 
 43 char *getDesDir(char *cmsg)
 44 {
 45         char *p;
 46 
 47         p=strtok(cmsg," ");
 48         p=strtok(NULL," ");
 49         return p;
 50 
 51 }
 52 
 53 
 54 void msg_handler(struct Msg msg,int fd)
 55 {
 56         char dataBuf[1280]={0};
 57         char *file = NULL;
 58         int fdfile;
 59 
 60         printf("cmd:%s\n",msg.data);
 61         int ret=get_cmd_type(msg.data);
 62 
 63         switch(ret){
 64                 case LS:
 65                 case PWD:
 66                         msg.type=0;
 67                         FILE *r=popen(msg.data,"r");
 68                         fread(msg.data,sizeof(msg.data),1,r);
 69                         write(fd,&msg,sizeof(msg));
 70                         break;
 71                 case CD:
 72                         msg.type=1;
 73                         char *dir=getDesDir(msg.data);
 74                         printf("dir:%s\n",dir);
 75                         chdir(dir);
 76                         break;
 77                 case GET:
 78                         file=getDesDir(msg.data);
 79 
  80                         if(access(file,F_OK)==-1){
 81                                 strcpy(msg.data,"NO This file!");
 82                                 write(fd,&msg,sizeof(msg));
 83                         }else{
 84                                 msg.type=DOFILE;
 85 
 86                                 fdfile=open(file,O_RDWR);
 87                                 read(fdfile,dataBuf,sizeof(dataBuf));
 88                                 close(fdfile);
 89 
 90                                 strcpy(msg.data,dataBuf);
 91                                 write(fd,&msg,sizeof(msg));
 92 
 93                                 }
 94                         break;
 95                 case PUT:
 96                         fdfile=open(getDesDir(msg.data),O_RDWR|O_CREAT,0666);
 97                         write(fdfile,msg.secondBuf,strlen(msg.secondBuf));
 98                         close(fdfile);
 99                         break;
100                 case QUIT:
101                         printf("client quit!\n");
102                         exit(-1);
103         }
104 
105 }
106 
107 int main(int argc ,char **argv)
108 {
109         int s_fd;
110         int c_fd;
111 
112         char readBuf[128];
113         int n_read;
114 
115 
116         struct sockaddr_in s_addr;
117         struct sockaddr_in c_addr;
118         struct Msg msg;
119 
120         if(argc!=3){
121                 puts("param is not good");
122                 exit(-1);
123         }
124         memset(&s_addr,0,sizeof(struct sockaddr_in));
125         memset(&c_addr,0,sizeof(struct sockaddr_in));
126 
127         //1 socket
128         s_fd = socket(AF_INET,SOCK_STREAM ,0);
129         if(s_fd==-1){
130                 perror("socket");
131                 exit(-1);
132         }
133 
134 
135         s_addr.sin_family=AF_INET;
136         s_addr.sin_port=htons(atoi(argv[2]));
137         inet_aton(argv[1],&s_addr.sin_addr);
138         //2 bind
139         bind(s_fd,(struct sockaddr*)&s_addr,sizeof(struct sockaddr_in));
140         //3 listen
141         listen(s_fd,10);
142 
143         //4 accept
144         int clen=sizeof(struct sockaddr_in);
145 
146         while(1)
147         {
148                 c_fd = accept(s_fd,(struct sockaddr*)&c_addr,&clen);
149                 if(c_fd==-1){
150                         perror("accept");
151                 }
152 
 153 
154                 printf("get connect: %s\n",inet_ntoa(c_addr.sin_addr));
155 
156 
157                 if(fork()==0){
158                 //5 read
159                         while(1)
160                         {
161                                 memset(msg.data,0,sizeof(msg.data));
162                                 n_read = read(c_fd,&msg,sizeof(msg));
163                                 if(n_read==0){
164                                         printf("client out\n");
165                                         break;
166                                 }else if(n_read>0){
167                                         msg_handler(msg,c_fd);
168                                         }
169                         }
170 
171                 }
172 
173         }
174         close(c_fd);
175         close(s_fd);
176         return 0;
177 }
                                                                                                                                            

FTP客户端

1 #include <stdio.h>
  2 #include <sys/types.h>          /* See NOTES */
  3 #include <sys/socket.h>
  4 //#include <linux/in.h>
  5 #include <arpa/inet.h>
  6 #include <netinet/in.h>
  7 #include <stdlib.h>
  8 #include <string.h>
  9 #include <fcntl.h>
 10 //#include "config.h"
 11 
 12 #define LS 0
 13 #define GET 1
 14 #define PWD 2
 15 #define IFGO 3
 16 #define LCD 4
 17 #define LLS 5
 18 #define CD 6
 19 #define PUT 7
 20 #define QUIT 8
 21 #define DOFILE 9
 22 
 23 struct Msg
 24 {
 25         int type;
 26         char data[1024];
 27         char secondBuf[128];
 28 };
 29 
 30 char *getdir(char *cmd)
 31 {
 32         char *p;
 33         p=strtok(cmd," ");
 34         p=strtok(NULL," ");
 35 
 36         return p;
 37 
 38 }
 39 
 40 int get_cmd_type(char *cmd)
 41 {
 42         if(!strcmp("lcd",cmd))   return LCD;
 43         if(!strcmp("quit",cmd))   return QUIT;
44         if(!strcmp("ls",cmd))   return LS;
 45         if(!strcmp("lls",cmd))   return LLS;
 46         if(!strcmp("pwd",cmd))   return PWD;
 47 
 48         if(strstr(cmd,"cd")!=NULL)   return CD;
 49         if(strstr(cmd,"get")!=NULL)   return GET;
 50         if(strstr(cmd,"put")!=NULL)   return PUT;
 51 
 52         return -1;
 53 }
 54 
 55 int cmd_handler(struct Msg msg,int fd)
 56 {
 57         char *dir=NULL;
 58         char buf[32];
 59         int ret;
 60         int filefd;
 61 
 62         ret=get_cmd_type(msg.data);
 63 
 64         switch(ret){
 65                 case LS:
 66                 case CD:
 67                 case PWD:
 68                         msg.type=0;
 69                         write(fd,&msg,sizeof(msg));
 70                         break;
 71                 case GET:
 72                         msg.type=2;
 73                         write(fd,&msg,sizeof(msg));
 74                         break;
 75                 case PUT:
 76                         strcpy(buf,msg.data);
 77                         dir=getdir(buf);
 78 
 79                         if(access(dir,F_OK)==-1){
 80                                 printf("%s not exsit\n",dir);
 81 
 82                         }else{
 83                                 filefd=open(dir,O_RDWR);
 84                                 read(filefd,msg.secondBuf,sizeof(msg.secondBuf));
 85                                 close(filefd);
 86 
 87                                 write(fd,&msg,sizeof(msg));
 88 
 89                                 }
 90                         break;
 91                 case LLS:
 92                         system("ls");
 93                         break;
 94                 case LCD:
 95                         dir=getdir(msg.data);
 96                         chdir(dir);
 97                         break;
 98                 case QUIT:
 99                         strcpy(msg.data,"quit");
100                         write(fd,&msg,sizeof(msg));
101                         close(fd);
102                         exit(-1);
103         }
104 
105         return ret;
106 
107 
108 }
109 
110 void handler_server_message(int c_fd,struct Msg msg)
111 {
112         int n_read;
113         struct Msg msgget;
114         int newfilefd;
115 
116         n_read=read(c_fd,&msgget,sizeof(msgget));
117 
118         if(n_read==0){
119                 printf("server is out, quite\n");
120                 exit(-1);
121         }
122         else if(msgget.type==DOFILE){
123                 char *p=getdir(msg.data);
124                 newfilefd=open(p,O_RDWR|O_CREAT,0600);
125                 write(newfilefd,msgget.data,strlen(msgget.data));
126                 putchar('>');
127                 fflush(stdout);
128         }
129         else{
130                 printf("--------------------------------------\n");
131                 printf("\n%s\n",msgget.data);
132                 printf("--------------------------------------\n");\
133                 putchar('>');
134                 fflush(stdout);
135         }
136 
137 }
138 
139 
140 int main(int argc, char** argv)
141 {
142         int c_fd;
143         struct sockaddr_in c_addr;
144 
145         struct Msg msg;
146 
147 
148          if(argc!=3){
149                 puts("param is not good");
150                 exit(-1);
151         }
152 
153 
154         memset(&c_addr,0,sizeof(struct sockaddr_in));
155 
156         //1 socket
157         c_fd = socket(AF_INET,SOCK_STREAM ,0);
158         if(c_fd==-1){
159                 perror("socket");
160                 exit(-1);
161         }
162 
163 
164         c_addr.sin_family=AF_INET;
165         c_addr.sin_port=htons(atoi(argv[2]));
166         inet_aton(argv[1],&c_addr.sin_addr);
167 
168         //2 connnect
169 
170         if(connect(c_fd,(struct sockaddr*)&c_addr,sizeof(struct sockaddr_in))==-1)
171         {
172                 perror("connect");
173                 exit(-1);
174         }
175         printf("connet ...\n");
176         int mark=0;
177         while(1)
178         {
179                 //3 send        
180                 memset(msg.data,0,sizeof(msg.data));
181                 if(mark==0)     printf(">");
182                 gets(msg.data);
183 
184                 if(strlen(msg.data)==0){
185                         if(mark==1){
186                                 printf(">");
187                         }
188                         continue;
189                 }
190                 mark=1;
191 
192                 int ret = cmd_handler(msg,c_fd);
193                 //4 read
194                 if(ret>IFGO){
195                         putchar('>');
196                         fflush(stdout);
197                         continue;
198                 }
199                 if(ret==-1){
200                         printf("command not \n");
201                         printf(">");
202                         fflush(stdout);
203                         continue;
204                 }
205                 handler_server_message(c_fd,msg);
206         }
207         return 0;
208 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值