/* inet_ntoa ... */
#include <arpa/inet.h>
+/* errno stuff ... */
+#include <errno.h>
+
+/* read, close */
+#include <unistd.h>
int main(int argc, char *argv[]) {
int listen_fd, send_fd;
- struct sockaddr_in local_addr, *remote_addr;
+ struct sockaddr_in local_addr, remote_addr;
+ socklen_t remote_addr_len;
int send_bytes, read_bytes;
if(argc!=2) {
exit(1);
}
- if(listen_fd=socket(AF_INET,SOCK_STREAM,0) == -1) {
+ if((listen_fd=socket(AF_INET,SOCK_STREAM,0)) == -1) {
printf("can't open socket.\n");
exit(1);
}
-
+
+ memset(&local_addr,0,sizeof(local_addr));
local_addr.sin_family=AF_INET;
local_addr.sin_port=htons(atoi(argv[1]));
local_addr.sin_addr.s_addr=htonl(INADDR_ANY);
- memset(&(local_addr.sin_zero),'\0',8);
- if(bind(listen_fd,(struct sockaddr)&local_addr,sizeof(struct sockaddr))==-1) {
- printf("unable to bind on port %d.\n",atoi(argv[1]);
+ if(bind(listen_fd,(struct sockaddr *)&local_addr,sizeof(local_addr))==-1) {
+ printf("unable to bind on port %d.\n",atoi(argv[1]));
+ perror("bind");
exit(1);
}
exit(1);
}
- if(send_fd=accept(listen_fd,(struct sockaddr *)remote_addr,
- sizeof(struct sockaddr_in)) {
+ remote_addr_len=sizeof(remote_addr);
+ if((send_fd=accept(listen_fd,(struct sockaddr *)&remote_addr,
+ &remote_addr_len))!=-1) {
printf("accepting connection from %s port %d.\n",
- inet_ntoa(remote_addr->sin_addr.s_addr),
- ntohs(remote_addr->sin_port));
+ inet_ntoa(remote_addr.sin_addr),
+ ntohs(remote_addr.sin_port));
/* send stuff .... */
read_bytes=1;
while(read_bytes>0) {
- char buf[1000];
- read_bytes=read(stdin,(void *)buf,sizeof(buf));
- send_bytes=send(send_fd,buf,sizeof(buf),0);
+ unsigned char buf[1000];
+ read_bytes=read(0,buf,sizeof(buf));
+ send_bytes=send(send_fd,buf,read_bytes,0);
}
close(send_fd);
+ close(listen_fd);
printf("connection closed ...\n");
- prinrf("%d from %d total bytes sent.\n",send_bytes,read_bytes);
- return 0;
+ printf("%d from %d total bytes sent.\n",send_bytes,read_bytes);
+ }
+ return 0;
}