⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpserver_serial.c

📁 The source code example of ARM9 development board from Artila (M-501 starter kit). The source code c
💻 C
字号:
/* *	Description:  *		Serial-to-Ethernet example. * *		A TCP Server is ready to accept connection *		TTY Port is set to 19200, N81, no flow control. * *		After connection is established,  *		TCP server will send the received package to TTY port,  *		and TTY port will send the received serial data to ethernet. *		 *		If remote client close the connection, TCP server will reture to LISTEN state. * *		Usage: ./test ttyPort listenPort *		e.g. ./tcpserver_serial /dev/ttyS1 4000 *		 *		matrix's tty port: *		ttyS1, ttyS2, ttyS3, ttyS4  * *	History: *	Create 2006/02/25 */ #include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h>#include <errno.h>#include <termios.h>#include <sys/types.h>#include <sys/socket.h>#include <arpa/inet.h>#include <netinet/in.h>#include <fcntl.h>struct termios T_old;int open_ttyPort(char *portName); 	/*open serial port*/int init_ttyPort(int sfd);		/*initialize serial port*/int init_tcpserver(int port);		/*initialize tcp server*/int main(int argc, char *argv[]){	int	fd_tty, fd_listen, fd_client;	int	tcpPort, addrlen, maxfd, ret;	struct sockaddr_in client_addr;	fd_set	rfd, wfd, efd; 	struct timeval	tm;		char buf[1024];		if ( argc != 3 ) {		printf("Usage: ./test ttyPort listenPort\n");        	return -1;        }         tcpPort = atoi(argv[2]);	/*get tcp server listen port*/        if (tcpPort < 0) {        	printf("invalid listen port");        	return -1;	}                fd_tty = open_ttyPort(argv[1]);	/*argv[1] contains tty port name that user input*/        if (fd_tty == -1)        	return -1;        	        if (init_ttyPort(fd_tty) == -1)        	return -1;        	                fd_listen = init_tcpserver(tcpPort);        if (fd_listen == -1)        	return -1;         	        fd_client = -1;        addrlen = sizeof(client_addr);        /*set select time out*/        tm.tv_sec = 3;		tm.tv_usec = 0;        	        while (1) {        	/*if connection is not established, call accept*/        	if (fd_client == -1) { 			printf("ready to accept...\n");				        		fd_client = accept(fd_listen, (struct sockaddr*)&client_addr, &addrlen);        		if (fd_client >= 0) {        			printf("accept: %s, port %d\r\n", inet_ntoa(client_addr.sin_addr), htons(client_addr.sin_port));        		}        	}        	if (fd_client < 0) /*connection is not established, continue to accept*/        		continue;        				FD_ZERO(&rfd);		FD_ZERO(&wfd);		FD_ZERO(&efd); 		FD_SET(fd_tty, &rfd);		FD_SET(fd_client, &rfd);		maxfd = (fd_tty > fd_client ? fd_tty : fd_client) + 1;		/*connection is established, call select to see if any data arrived*/		ret = select(maxfd, &rfd, &wfd, &efd, &tm);		if (ret < 0) {			printf("select fail\r\n");			break;		} else if (ret == 0) {			continue;	/*no data arrived, continue to call select*/		}		if (FD_ISSET(fd_tty, &rfd)) {	/*tty port has data to be read*/			ret = read(fd_tty, buf, sizeof(buf));			if (ret <= 0) {				printf("read tty port fail\r\n");				break;			}			if (send(fd_client, buf, ret, 0) < 0) {	/*send data to ethernet*/				printf("send to ethernet fail\r\n");				break;							}		}		if (FD_ISSET(fd_client, &rfd)) { /*tcp client has data to be read*/			ret = recv(fd_client, buf, sizeof(buf), 0);			if (ret < 0) {				printf("read from ethernet fail\r\n");				break;			} else if (ret == 0) {				printf("disconnect by remote\r\n");				close(fd_client);				fd_client = -1;				continue; /*continue to accept...*/			}			if (write(fd_tty, buf, ret) < 0) { /*send data to tty port*/				printf("write to tty fail\r\n");				break;							}					}	}	close(fd_listen);	close(fd_client);	tcsetattr(fd_tty,TCSANOW,&T_old);	close(fd_tty);	printf("bye\r\n");	return -1;}intopen_ttyPort(char *portName){	int fd;	fd = open(portName, O_RDWR | O_NOCTTY | O_NDELAY);	if (fd == -1) {		perror("open_port: Unable to open - ");		return (fd);	}	fcntl(fd, F_SETFL, 0);	return (fd);}intinit_ttyPort(int sfd){	struct termios T_new;        if (tcgetattr(sfd,&T_old) != 0) {		printf("tcgetattr failed\n");		close(sfd);  		return -1;        }         T_new = T_old;        T_new.c_cflag = (B19200 | CS8 | CREAD | CLOCAL | HUPCL);        T_new.c_oflag = 0;        T_new.c_iflag = 0;        T_new.c_lflag = 0;        if (tcsetattr(sfd,TCSANOW,&T_new) != 0) {		printf("tcsetattr failed\n");		tcsetattr(sfd,TCSANOW,&T_old);		close(sfd);		return -1;         }         return 0; }intinit_tcpserver(int port){	int sockfd, flag, ret;	struct sockaddr_in dest;			sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);	if (sockfd < 0) {  		printf("socket failed\n");  		return (-1);			}	bzero(&dest, sizeof(dest));	dest.sin_family = AF_INET;	dest.sin_port = htons(port);	dest.sin_addr.s_addr = htonl(INADDR_ANY);	bind(sockfd, (struct sockaddr*)&dest, sizeof(dest));	listen(sockfd, 1);		return sockfd;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -