实验程序1

来源:互联网 发布:js怎么调用java方法 编辑:程序博客网 时间:2024/05/16 06:48
 

#include<errno.h>

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/types.h>

#include<sys/stat.h>

#include <termios.h>

#include <fcntl.h>

#include <time.h>

#include <unistd.h>

#include <ctype.h>

#include<netdb.h>

#include<netinet/in.h>

#include<sys/socket.h>

#define HOST_COM_PORT      0

#define TARGET_COM_PORT  1

#define BUFFER_SIZE                1024

#define MAX_COM_NUM                 5

#define GNR_COM                   1

#define COM_TYPE                   1

 

 

 

void *NettoSerThread(void *arg);

void *SertoNetThread(void *arg);

        

char buffer[1024];                                         //NettoSerThread线程数据缓冲区

char buff[1024];                                             //SertoNetThread线程数据缓冲区

        

int sockfd;                                                                 //网络设备文件描述符

int uartfd;                                                                 //串口设备文件描述符

struct sockaddr_in client_addr;      //客户端地址结构体变量

 

/*网络套接字函数*/

/*服务器端开始创建套接字描述符*/

void SocketInit(int portnumber)

{

         struct sockaddr_in server_addr;     //服务器端地址结构体变量

         if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)

         {

                   fprintf(stderr,"socket error:%s\n\a",strerror(errno));

                   exit(1);

         }

        

         /*服务器端填充sockaddr结构*/

         bzero(&server_addr,sizeof(struct sockaddr_in));/*初始化,置0*/

         server_addr.sin_family=AF_INET;//IPv4

         server_addr.sin_addr.s_addr=htonl(INADDR_ANY);//服务器程序可以运行在任何ip的主机上

         //server_addr.sin_addr.s_addr=inet_addr("192.168.1.1");

         server_addr.sin_port=htons(portnumber);

        

         /*捆绑sockfd描述符到ip地址*/

         if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1)

         {

                   fprintf(stderr,"bind error:%s\n\a",strerror(errno));

                   exit(1);

         }

        

         /*设置允许连接的最大客户数*/

         if(listen(sockfd,5)==-1)

         {

                   fprintf(stderr,"listen error:%s\n\a",strerror(errno));

                   exit(1);

         }

}

 

int main()

{

         pthread_t NettoSer_id,SertoNet_id;       //网络接收串口发送线程标示符,串口接收网络发送线程标示符

         /*打开文件*/

         if((uartfd=open(RECV_FILE_NAME,O_CREAT|O_WRONLY,0664))<0)

         {

                   perror("open error");

                   return 1;

         }

         memset(buffer,0,1024);

         memset(buff,0,1024);

        

         /*创建两个线程*/

         ret=pthread_create(&NettoSer_id,NULL,NettoSerThread,NULL);

         if(ret!=0)

         {

                   printf("Create NettoSerThread thread error\n");

                   return ret;

         }

         ret=pthread_create(&SertoNet_id,NULL,SertoNetThread,NULL);

         if(ret!=0)

         {

                   printf("Create SertoNetThread thread error\n");

                   return ret;

         }

         pthread_join(NettoSer_id,NULL);

         pthread_join(SertoNet_id,NULL);

         close(uartfd);

         /*结束通讯*/

         close(sockfd);

         return 0;

}

 

    /*网络接收数据,串口发送数据线程函数*/

         /*创建连接套接字,从网口读数据,判断数据的长度,再从串口发送数据*/

         void *NettoSerThread(void *arg)

         {

                   int new_fd;                                                                        //连接的套接字定义

                   int nbytes;

                   int sin_size;

                   SocketInit(3333);

                   while(1)

                   {

                            /*服务器阻塞,直到客户程序建立连接*/

                            sin_size=sizeof(struct sockaddr_in);

                            if(new_fd=accept(sockfd,(struct sockaddr*)(&client_addr),&sin_size)==-1)

                            {

                                     fprintf(stderr,"server error:%s\n\a",strerror(errno));

                                     exit(1);

                            }

                            fprintf(stderr,"server get connecttion from %s\n",inet_ntoa(client_addr.sin_addr));

                            /*从网口读数据读到buffer缓冲区每次读1KB*/

                            if((nbytes=read(new_fd,buffer,1024))==-1)

                            {

                                     fprintf(stderr,"read error:5s\n",strerror(errno));

                                     exit(1);

                            }

                            printf("server received %s\n",buffer);

                            /*将buffer缓冲区中的数据发送至串口*/

                            write(uartfd,buffer,1024);

                            printf("server received %s\n",buffer);

                            /*这个通讯已经结束*/

                            close(new_fd);

                            /*循环下一个*/

                   }

                            pthread_exit(NULL);

         }

        

         /*串口读取数据,网口发送数据*/

         void *SertoNetThread(void *arg)

         {

                   SocketInit(5555);

                   do

                   {

                            /*从串口中读数据读入buff缓冲区中*/

                            if(read(uartfd,buff,1024)>0)

                            {

                                     printf("the receive words are:%s",buff);

                            }

                            /*将buff缓冲区中的数据发送到网口*/

                            //write();

                   }while(strncmp(buff,"quit",4));

                   pthread_exit(NULL);

         }