Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
UDPecho.c
Package: linux-socket-program.rar [view]
Upload User: wei_4586
Upload Date: 2008-05-28
Package Size: 18k
Code Size: 1k
Category:
Linux Network
Development Platform:
Unix_Linux
- /* UDPecho.c - main, UDPecho */
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- extern int errno;
- int UDPecho(const char *host, const char *service);
- int errexit(const char *format, ...);
- int connectUDP(const char *host, const char *service);
- #define LINELEN 128
- /*------------------------------------------------------------------------
- * main - UDP client for ECHO service
- *------------------------------------------------------------------------
- */
- int
- main(int argc, char *argv[])
- {
- char *host = "localhost";
- char *service = "echo";
- switch (argc) {
- case 1:
- host = "localhost";
- break;
- case 3:
- service = argv[2];
- /* FALL THROUGH */
- case 2:
- host = argv[1];
- break;
- default:
- fprintf(stderr, "usage: UDPecho [host [port]]n");
- exit(1);
- }
- UDPecho(host, service);
- exit(0);
- }
- /*------------------------------------------------------------------------
- * UDPecho - send input to ECHO service on specified host and print reply
- *------------------------------------------------------------------------
- */
- int
- UDPecho(const char *host, const char *service)
- {
- char buf[LINELEN+1]; /* buffer for one line of text */
- int s, nchars; /* socket descriptor, read count*/
- s = connectUDP(host, service);
- while (fgets(buf, sizeof(buf), stdin)) {
- buf[LINELEN] = ''; /* insure null-terminated */
- nchars = strlen(buf);
- (void) write(s, buf, nchars);
- if (read(s, buf, nchars) < 0)
- errexit("socket read failed: %sn",
- strerror(errno));
- fputs(buf, stdout);
- }
- }