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

📄 example10_2.c

📁 LinuxNetProgramming Linux网络编程基础实例
💻 C
字号:
/* example10_2.c */

#include <sys/types.h> 

#include <sys/socket.h> 

#include <string.h> 

#include <sys/un.h>

#include <sys/time.h>

#include <fcntl.h>

#define SOCKETNAME  "/faculty/rossa/system/mysocket"



int

main(void){

	int s;					/* This end of connection*/	

int len;					/* length of sockaddr */

	int nread;				/* return from read() */	

int nready;				/* # fd's ready. */

	int maxfd;				/* fd's 0 to maxfd-1 checked. */ 	

char buf[1024];

	fd_set fds;				/* set of file descriptors to check. */	

struct sockaddr_un name;

	if( (s = socket(AF_UNIX, SOCK_STREAM, 0) ) < 0) {		

perror("socket");		

exit(1);

}

		/*Create the address of the server.*/

	memset(&name, 0, sizeof(struct sockaddr_un));	

name.sun_family = AF_UNIX;

	strcpy(name.sun_path, SOCKETNAME);

	len = sizeof(name.sun_family) + strlen(name.sun_path);

		/*Connect to the server.*/	

if (connect(s, (struct sockaddr *) &name, len) < 0) {

		perror("connect");

		exit(1);	

}

maxfd = s + 1;

while(1) {		

/* Set up polling. */

		FD_ZERO(&fds);

		FD_SET(s,&fds);

		FD_SET(0,&fds);

			/* Wait for some input. */

		nready = select(maxfd, &fds, (fd_set *) 0, (fd_set *) 0,

				(struct timeval *) 0);		

/* If either device has some input, 

read it and copy it to the other. */

if( FD_ISSET(s, &fds)) {

			nread = recv(s, buf, sizeof(buf), 0);

/* If error or eof, terminate. */

			if(nread < 1) {

close(s);

exit(0);

}

write(1, buf, nread);

}

if( FD_ISSET(0, &fds)) {

			nread = read(0, buf, sizeof(buf));

				/* If error or eof, terminate. */			

if(nread < 1) {

				close(s);

				exit(0);

			}			

send( s, buf, nread, 0);

}

}

}

⌨️ 快捷键说明

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