heres the new code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define PORT 9017
//this is the default port that will be used if program is run w/o the -p switch
#define BUFFER 1024
//how much data can be held in a single buffer
#define MAXUSERS 10
//basically, the backlog for the listen() function
int main(int argc, char* argv[])
{
/* Variables used throughout the program */
int sockfd, newfd; // file descriptors for the server and incoming connection
struct sockaddr_in myaddr; // sockets for the connections
struct sockaddr_in theiraddr;
int port = -1;
int i;
int data;
int nfound;
char* buffer = malloc(BUFFER);
int addrlen;
int maxfd;
if (argc > 1)
{
int i;
for (i = 0; i < 2; i++)
{
if (strncmp(argv[i], "-p", 2) == 0)
{
if (argv[i + 1])
{
if (atoi(argv[i + 1]) > 0)
{
port = atoi(argv[i + 1]);
}
if (atoi(argv[i + 1]) <= 0)
{
port = atoi(argv[i + 1]) * -1;
}
}
else
{
fprintf(stderr, "\nPort not provided!");
fprintf(stderr, "\nUsage: echoserv -p <port> -h\n");
fprintf(stderr, "-p <port> sets the port that host server\n");
fprintf(stderr, "-h prints this help screen\n\n");
exit(EXIT_FAILURE);
}
}
if (strncmp(argv[i], "-h", 2) == 0)
{
fprintf(stderr, "\nUsage: echoserv -p <port> -h\n");
fprintf(stderr, "-p <port> sets the port that host server\n");
fprintf(stderr, "-h prints this help screen\n\n");
exit(EXIT_SUCCESS);
}
}
}if (port == -1)
port = PORT;
printf("\n***ECHOSERV v0.0.2***\n");
printf("***Port is: %d***\n", port);
fd_set master;
fd_set slave;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(EXIT_FAILURE);
}
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(port);
myaddr.sin_addr.s_addr = INADDR_ANY;
if ((bind(sockfd, (struct sockaddr *)&myaddr, sizeof(struct sockaddr)) == -1))
{
perror("bind");
exit(EXIT_FAILURE);
}
if ((listen(sockfd, MAXUSERS)) == -1)
{
perror("listen");
exit(EXIT_FAILURE);
}
FD_ZERO(&master);
FD_ZERO(&slave);
FD_SET(sockfd, &master);
maxfd = sockfd;
for( ; ; ) /* forever and ever and ever and ever... */
{
slave = master;
if ((nfound = select(maxfd + 1, &slave, NULL, NULL, NULL)) < 0)
{
perror("select");
exit(EXIT_FAILURE);
}
for (i = 0; i <= maxfd; i++)
{
if (FD_ISSET(i, &slave)) //if there are connections
{
if (i == sockfd)
{
addrlen = sizeof(theiraddr);
if ((newfd = accept (sockfd, (struct sockaddr *)&theiraddr, &addrlen)) == -1)
{
perror("accept");
}
}
else
{
FD_SET(newfd, &master); //place all new connections in master set
if (newfd > maxfd) //update the maxfd variable
maxfd = newfd;
FD_CLR(sockfd, &slave);
}
}
else
{
if ((data = recv(i, buffer, BUFFER , 0)) == -1)
{
perror("recv");
}
if (data <= 0) //disconnection or error
{
FD_CLR(i, &master); // cya later dude!
close(i);
}
else
{
if ((send(i, buffer, data , 0)) == -1)
{
perror("send");
} //copycat
}
}
}
}
return 0;
}
heres what happens when i run:
echoserv -p 9028
i telnet onto port 9028. perror prints
recv: socket operation on non-socket
three times and then if i open another telnet connection no more stuff is printed with perror.
im a bit confused about this.
thx for help