1 /* stream.c - streaming server
13 /* socket and bind stuff */
14 #include <sys/types.h>
15 #include <sys/socket.h>
18 #include <netinet/in.h>
21 #include <arpa/inet.h>
29 int main(int argc, char *argv[]) {
30 int listen_fd, send_fd;
32 struct sockaddr_in local_addr, remote_addr;
33 int send_bytes, read_bytes;
36 printf("usage: %s <port>\n",argv[0]);
40 if((listen_fd=socket(AF_INET,SOCK_STREAM,0)) == -1) {
41 printf("can't open socket.\n");
45 memset(&local_addr,0,sizeof(local_addr));
46 local_addr.sin_family=AF_INET;
47 local_addr.sin_port=htons(atoi(argv[1]));
48 local_addr.sin_addr.s_addr=htonl(INADDR_ANY);
49 //memset(&(local_addr.sin_zero),'\0',8);
51 if(bind(listen_fd,(struct sockaddr *)&local_addr,sizeof(local_addr))==-1) {
52 printf("unable to bind on port %d.\n",atoi(argv[1]));
57 if(listen(listen_fd,1)==-1) {
58 printf("error listening on port %d.\n",atoi(argv[1]));
61 len=sizeof(remote_addr);
62 if( (send_fd=accept(listen_fd,(struct sockaddr *)&remote_addr,&len)) <0) {
63 printf("accepting connection from %s port %d.\n",
64 inet_ntoa(remote_addr.sin_addr),
65 ntohs(remote_addr.sin_port));
70 unsigned char buf[1000];
71 read_bytes=read(0,buf,sizeof(buf));
72 send_bytes=send(send_fd,buf,sizeof(buf),0);
76 printf("connection closed ...\n");
77 printf("%d from %d total bytes sent.\n",send_bytes,read_bytes);