nbsp; struct sockaddr_in serverName = { 0 }, clientName = { 0 }; if (2 != argc) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(1); } myPort = atoi(argv[1]); udpSocket = socket(PF_INET, SOCK_DGRAM, /* PF_INET和SOCK_DGRAM组合代表了UDP */ IPPROTO_UDP); if (-1 == udpSocket) { perror("socket()"); exit(1); } memset(&serverName, 0, sizeof(serverName)); memset(&clientName, 0, sizeof(clientName)); serverName.sin_family = AF_INET; serverName.sin_addr.s_addr = htonl(INADDR_ANY); serverName.sin_port = htons(myPort); status = bind(udpSocket, (struct sockaddr *) &serverName, sizeof(serverName)); if (-1 == status) { perror("bind()"); exit(1); } for (;;) { /* ssize_t recvfrom(int s, void *buf, size_t len, int flags, struct sock- * addr *from, socklen_t *fromlen); * / size = recvfrom(udpSocket, mesg, MAX_MESG_SIZE, 0, (struct sockaddr *) &clientName, &clientLength); if (size == -1) { perror("recvfrom()"); exit(1); } /* ssize_t sendto(int s, const void *msg, size_t len, int flags, const * struct sockaddr *to, socklen_t tolen); */ status = sendto(udpSocket, mesg, size, 0, (struct sockaddr *) &clientName, clientLength); if (status != size) { fprintf(stderr, "sendto(): short write.\n"); exit(1); } } /* never reached */ return 0; } 将TCP的客户端改写成UDP的客户端将留作一个练习 4、/etc/services文件 为了连接到一个服务端,你必须首先知道其监听的地址和端口。许多常见的服务(FTP,TELNET等等)的信息都列在了一个文本文 件/etc/services中。getservbyname()函数可以用名称询问一个服务的详细情况包括它的端口号(注意!它已经是网络字节序了),它的原型在 /usr/include: struct servent *getservbyname(const char *name, const char *proto); 默认的proto是"tcp" struct servent { char *s_name; /* official service name */ char **s_aliases; /* alias list */ int s_port; /* port number, network * byte-order--so do not * use host-to-network macros */ char *s_proto; /* protocol to use */ }; 5、总结 这篇文章介绍了Linux下使用C和BSD Socket API进行网络编程。总的来说,用这套API来写代码将是相当耗费劳力的,特别是与其他一些 技术相比。在今后的文章中,我会将BSD Socket API与另外两个可以选择的Linux下的方法进行比较:远程过程调用(RPCs,Remote Procedure Calls)和CORBA(Common Object Request Broker Achitecture,公用对象请求代理[调度]程序体系结构)。RPCs在Ed Petron的文章"Remote Procedure Calls"(Linux Journal Issue #42,1997年十月)中有介绍。 相关资源: UNIX Network Programming, W. Richard Steves, Prentice Hall, 1990. An Introductory 4.4BSD Interprocess Communication Tutorial, Stuart Sechrest, University of California, Berkeley. 1986. Available via anonymous FTP at: ftp://ftp.NetBSD.ORG/pub/NetBSD/misc/lite2-docs/psd/20.ipctut.ps.gz. An Advanced 4.4BSD Interprocess Communication Tutorial, Samuel J. Leffler, Robert S. Fabry, William N. Joy, Phil Lapsley, University of California, Berkeley. 1986 Available via anonymous FTP at: ftp://ftp.NetBSD.ORG/pub/NetBSD/misc/lite2- docs/psd/21.ipc.ps.gz. Java and the Client-Server, Joe Novosel, Linux Journal, Issue 33, January 1997. RFC 793: Transmission Control Protocol, J. Postel (ed.), September 1981. Available via HTTP at http://www.internic.net/rfc/rfc793.txt. RFC 1337: TIME-WAIT Assassination Hazards in TCP, R. Braden, May 1992. Available via HTTP at http://www.internic.net/rfc/rfc1337.txt. Programming UNIX Sockets in C FAQ, Vic Metcalfe, Andrew Gierth and other contributers, February 1997. Available via HTTP at http://kipper.york.ac.uk/~vic/sock-faq/html/unix-socket-faq.html 作者简介: Ivan Griffin is a research postgraduate student in the ECE department at the University of Limerick, Ireland. His interests include C++/Java, WWW, ATM, the UL Computer Society (http://www.csn.ul.ie/) and, of course, Linux (http://www.trc.ul.ie/~griffini/linux.html). His e-mail address is ivan.griffin@ul.ie. Dr. John Nelson is a senior lecturer in Computer Engineering at the University of Limerick. His interests include mobile communications, intelligent networks, Software Engineering and VLSI design. His e-mail address is john.nelson@ul.ie. 所有的程序都可以在这里获得: ftp://ftp.ssc.com/pub/lj/listings/issue46/2333.tgz
上一页 [1] [2] |