Socket operation on non-socket C - c

I am getting this error when trying to make an application to connect and listen to a port for data.
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char * arg[]){
int conn_s = socket(AF_INET, SOCK_STREAM, 0); //Create the socket
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(1234);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
int res = bind(conn_s, (struct sockaddr *) &servaddr, sizeof(servaddr));
if(res < 0){
printf("Error has occured\n");
}
FILE * stream = fdopen(conn_s, "w+"); //Create a stream for the socket
FILE * file = fopen("response2.txt", "w+"); //Create a file to store the output of the stream
char * line = NULL; //Where each line of the stream will be stored in
size_t len = 0; // The length of the line
ssize_t bytes; //The size of the line in bytes
int lis = listen(conn_s, SOMAXCONN);
fcntl(lis, F_SETFL, O_NONBLOCK);
while(1) {
conn_s = accept(lis, NULL, NULL);
if(conn_s < 0){
if((errno == EAGAIN) || (errno == EWOULDBLOCK))
continue;
perror("Failed to accept connection");
exit(EXIT_FAILURE);
}
long conn_s_flags = fcntl(conn_s, F_GETFL);
fcntl(conn_s, F_SETFL, conn_s_flags & ~O_NONBLOCK);
while((bytes = getline(&line, &len, stream)) != -1) {
printf("%s\n", line);
fwrite(line, sizeof(char), bytes, file);
}
close(conn_s);
}
free(line);
return 0;
}
I am trying to connect to port 1234, listen to it and accept it to recieve data, but the error keeps occuring.
Also I am trying to test using netcat, but get a different error whenever nc is running on the port I specified.
Thanks

int lis = listen(conn_s, SOMAXCONN);
fcntl(lis, F_SETFL, O_NONBLOCK);
while(1) {
conn_s = accept(lis, NULL, NULL);
listen() does not return a socket FD. It returns zero or -1. The second line is therefore erroneous, as is the following accept() call. It should be:
if (listen(conn_s, SOMAXCONN) == -1)
{
perror("listen");
return; // or whatever
}
fcntl(conn_s, F_SETFL, O_NONBLOCK);
while(1) {
int conn_c = accept(conn_s, NULL, NULL);
NB Don't lose conn_s by storing the result of accept() into it.

Related

send file from client to server using socket in c on linux

I'm very new in C language and linux and English is not my mother language. Sorry for those in advance.
What I need to is to send .avi or .mp4 files from a client to a server using socket on Linux. I can send the file from client to server but the video on the server is not working.
When I try to play the video, I keep getting an error like "could not determine the type of stream". When I checked the original video on client, the size was 5,787,969 bytes but the size of the video transferred on server is 5,786,954 bytes. I think this is because of data loss when I transfer the file.
How do I fix this?
This is my code below:
Server
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // read, write
#include <arpa/inet.h>
#include <sys/types.h> // socket, bind, accept, open
#include <sys/socket.h> // socket, bind, listen, accept
#include <sys/stat.h> // open
#include <fcntl.h> // open
#include <errno.h>
#define PORT 5500
#define MAXBUF 1024
int main() {
int server_sockfd;
int client_sockfd;
int des_fd; // file num
struct sockaddr_in serveraddr, clientaddr;
int client_len, read_len, file_read_len; // length
char buf[MAXBUF];
int check_bind;
client_len = sizeof(clientaddr);
/* socket() */
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(server_sockfd == -1) {
perror("socket error : ");
exit(0);
}
/* bind() */
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(PORT);
if(bind(server_sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) > 0) {
perror("bind error : ");
exit(0);
}
/* listen */
if(listen(server_sockfd, 5) != 0) {
perror("listen error : ");
}
while(1) {
char file_name[MAXBUF]; // local val
memset(buf, 0x00, MAXBUF);
/* accept() */
client_sockfd = accept(server_sockfd, (struct sockaddr *)&clientaddr, &client_len);
printf("New Client Connect : %s\n", inet_ntoa(clientaddr.sin_addr));
/* file name */
read_len = read(client_sockfd, buf, MAXBUF);
if(read_len > 0) {
strcpy(file_name, buf);
printf("%s > %s\n", inet_ntoa(clientaddr.sin_addr), file_name);
} else {
close(client_sockfd);
break;
}
/* create file */
des_fd = open(file_name, O_WRONLY | O_CREAT | O_EXCL, 0700);
if(!des_fd) {
perror("file open error : ");
break;
}
/* file save */
while(1) {
memset(buf, 0x00, MAXBUF);
file_read_len = read(client_sockfd, buf, MAXBUF);
write(des_fd, buf, file_read_len);
if(file_read_len == EOF | file_read_len == 0) {
printf("finish file\n");
break;
}
}
close(client_sockfd);
close(des_fd);
}
close(server_sockfd);
return 0;
}
Client
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PORT 5500
#define IP "127.0.0.1"
#define MAXBUF 1024
int main() {
struct sockaddr_in serv_addr;
int s;
int sourse_fd;
char buf[MAXBUF];
int file_name_len, read_len;
/* socket() */
s = socket(AF_INET, SOCK_STREAM, 0);
if(s == -1) {
return 1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(IP);
serv_addr.sin_port = htons(PORT);
if(connect(s, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) {
perror("connect : ");
printf("fail to connect.\n");
close(s);
return 1;
}
memset(buf, 0x00, MAXBUF);
printf("write file name to send to the server: ");
scanf("%s", buf);
printf(" > %s\n", buf);
file_name_len = strlen(buf);
send(s, buf, file_name_len, 0);
sourse_fd = open(buf, O_RDONLY);
if(!sourse_fd) {
perror("Error : ");
return 1;
}
while(1) {
memset(buf, 0x00, MAXBUF);
read_len = read(sourse_fd, buf, MAXBUF);
send(s, buf, read_len, 0);
if(read_len == 0) {
break;
}
}
return 0;
}
In Server
Look at your "file save" while loop in Server:
/* file save */
while(1) {
memset(buf, 0x00, MAXBUF);
file_read_len = read(client_sockfd, buf, MAXBUF);
write(des_fd, buf, file_read_len);
if(file_read_len == EOF | file_read_len == 0) {
printf("finish file\n");
break;
}
}
Instead of file_read_len == EOF | file_read_len == 0 you should use only file_read_len == 0.
Still in Server, inside open() function that creates the file, I also recommend to change 0700 to S_IRWXU & (~S_IXUSR) __but_it_is_not_mandatory__. See user#host:~$ man 2 open for more details.
In Client
In Client you only need to add "+ 1" in strlen(buf):
file_name_len = strlen(buf) + 1;
That is because a string needs to end with a null byte. And strlen() returns the length of string minus the null byte at the end :-)
I also recommend you to use argc and argv to get arguments, look:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int
main (int argc, char **argv) {
int how_many_files = argc - 1; /* '- 1' cause first arg is program name
*/
int fd; /* file descriptor */
int i; /* a counter */
char buffer[10]; /* a buffer */
for (i = 0; i < how_many_files; i++) {
fd = open(argv[i + 1], O_RDONLY);
if (fd == -1)
continue;
read(fd, buffer, 10);
buffer[9] = '\0'; /* last byte of buffer needs to be null char*/
printf("First 10 Bytes of the file is: %s\n",
buffer);
close(fd);
}
return 0;
}

TCP server/client how to keep connections alive?

I wrote a simple TCP echo server to handle multiple clients. It uses select() to get multiple connections.
Server Code:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
int create_listener(uint16_t port) {
int listen_fd;
struct sockaddr_in name;
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd < 0) {
perror ("socket");
exit(EXIT_FAILURE);
}
bzero(&name, sizeof(name));
name.sin_family = AF_INET;
name.sin_addr.s_addr = htonl(INADDR_ANY);
name.sin_port = htons(port);
if (bind(listen_fd, (struct sockaddr *) &name, sizeof(name)) < 0) {
perror ("bind");
exit(EXIT_FAILURE);
}
return listen_fd;
}
int read_from_client(int fd) {
char buffer[100];
int nbytes;
nbytes = read(fd, buffer, 100);
if (nbytes < 0) {
perror("read");
exit(EXIT_FAILURE);
}
else if (nbytes == 0) {
return -1;
}
else {
fprintf(stderr, "Server: got message: %s\n", buffer);
write(fd, buffer, strlen(buffer) + 1);
return 0;
}
}
int main(int argc, char *argv[]) {
int listen_fd;
uint16_t port = 22000;
fd_set active_fd_set, read_fd_set;
int i;
struct sockaddr_in servaddr;
/* Create the socket and set it up to accept connections. */
listen_fd = create_listener(port);
if (listen(listen_fd, 10) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
/* Initialize the set of active sockets. */
FD_ZERO(&active_fd_set);
FD_SET(listen_fd, &active_fd_set);
while (1) {
/* Block until input arrives on one or more active sockets. */
read_fd_set = active_fd_set;
if (select(FD_SETSIZE, &read_fd_set, NULL, NULL, 0) < 0) {
perror("select");
exit(EXIT_FAILURE);
}
/* Service all the sockets with input pending. */
for (i = 0; i < FD_SETSIZE; ++i) {
if (FD_ISSET(i, &read_fd_set)) {
if (i == listen_fd) {
/* Connection request on original socket. */
int new_fd;
new_fd = accept(listen_fd, (struct sockaddr *) NULL, NULL);
if (new_fd < 0) {
perror ("accept");
exit(EXIT_FAILURE);
}
FD_SET(new_fd, &active_fd_set);
}
else {
/* Data arriving on an already-connected socket. */
if (read_from_client(i) < 0) {
close(i);
FD_CLR(i, &active_fd_set);
}
}
}
}
}
return 0;
}
Client code:
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
int sockfd, n;
char sendline[100];
char recvline[100];
struct sockaddr_in servaddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(22000);
inet_pton(AF_INET, "127.0.0.1", &(servaddr.sin_addr));
connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
while (1) {
bzero(sendline, 100);
bzero(recvline, 100);
fgets(sendline, 100, stdin);
write(sockfd, sendline, strlen(sendline) + 1);
read(sockfd, recvline, 100);
printf("%s", recvline);
}
return 0;
}
The problem is when I run server in one terminal and run two clients in another two terminals. If I use Ctrl+C to terminate one client, the server automatically terminates. I'm wondering why the server acts this way. What I'm expecting is the server runs forever. When client 1 terminates, server should still has a live connection with client 2.
Looks like you're hitting the exit in read_from_client. In general, in a server that serves multiple clients, you probably don't want to exit when you have a failure with one of the client connections.

writing to a close socket didn't raise a SIGPIPE as expected

I've already read about how to prevent SIGPIPE, then I write a small program to test it. Here is the code.
server.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void hdl(int sig_num, siginfo_t *sig_info, void *context)
{
printf("got you, SIGPIPE!\n");
}
int main()
{
int sfd, cfd;
struct sockaddr_in saddr, caddr;
struct sigaction act;
memset (&act, '\0', sizeof(act));
act.sa_sigaction = hdl;
act.sa_flags = SA_SIGINFO;
if (sigaction(SIGPIPE, &act, NULL) < 0) {
return 1;
}
sfd= socket(AF_INET, SOCK_STREAM, 0);
saddr.sin_family=AF_INET;
saddr.sin_addr.s_addr=inet_addr("192.168.22.91");
saddr.sin_port=htons(12345);
if(bind(sfd, (struct sockaddr *)&saddr, sizeof(saddr)) )
{
printf("bind error\n");
return -1;
}
if(listen(sfd, 1))
{
printf("error\n");
return -1;
}
char buf[1024] = {0};
while(1) {
printf("Server listening...\n");
cfd=accept(sfd, (struct sockaddr *)NULL, NULL);
fcntl(cfd, F_SETFL, O_NONBLOCK);
int size = read(cfd, buf, 1024);
if(size == -1)
printf("read error\n");
sleep(2); // sleep for a while to make sure the client closed the socket
int ret;
if((ret = write(cfd, buf, strlen(buf)))<0)
{
if(errno == EPIPE)
fprintf(stderr, "SIGPIPE");
}
ret = write(cfd, buf, strlen(buf)); // write again.
printf("write return %d\n", ret);
}
close(sfd);
}
client.c
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <assert.h>
int main()
{
int ret, fd;
struct sockaddr_in sa_dst;
char buffer[] = "hello, world";
char rcv_buf[128] = {0};
fd = socket(AF_INET, SOCK_STREAM, 0);
memset(&sa_dst, 0, sizeof(struct sockaddr_in));
sa_dst.sin_family = AF_INET;
sa_dst.sin_port = htons(12345);
sa_dst.sin_addr.s_addr = inet_addr("192.168.22.91");
ret = connect(fd, (struct sockaddr *)&sa_dst, sizeof(struct sockaddr));
if(ret != -1)
{
send(fd, buffer, strlen(buffer), 0);
close(fd);
}
return 0;
}
When I run the server and the client on the same linux machine, on the server side, the first write() returns the number of bytes written while I expect a SIGPIPE signal because I closed the socket on the client side, the second write() does generate a SIGPIPE signal.
But when I ran the client on another linux machine or on a Windows machine(implement the same client with Winsock), I did't catch any SIGPIPE signal, and the second write() still returns the size of the buffer. Can someone tell me what's going on?
It can't happen on the first write, for two reasons:
The localhost doesn't know that the peer has closed the socket for reading. A FIN has been received but that could just be because the peer has shutdown for output. Only an RST will tell it that, and it doesn't get that util the next I/O at the earliest.
Buffering.
NB you're corrupting the value of errno by calling perror(), so testing it afterwards isn't valid.
Just Change this in SERVER and it will work
fcntl(cfd, F_SETFL, O_NONBLOCK);
int size = read(cfd, buf, 1024);
if(size == -1)
printf("read error\n");
sleep(2); // sleep for a while to make sure the client closed the socket
int ret;
ret = write(cfd, buf, strlen(buf));
sleep(2);
ret = write(cfd, buf, strlen(buf)); // write again.
printf("write return %d\n", ret);

Creating a file transfer application using UDP in C

I am trying to implement a server-client application in which the client sends the name of file to server. The server looks for the file and if the file exist it sends the file to the client back and this goes on . Here is the code i have written but the problem is that the server does not send the content of file back to client I had the same problem earlier ,then i re-wrote the code .
Here is the code :-
Server Side
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
int main() {
char buff[20];
char content[200];
int sd, connfd, len, bytes_read;
struct sockaddr_in servaddr, cliaddr;
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd == -1) {
puts("socket not created in server");
return 1;
} else {
puts("socket created in server");
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(7802);
if (bind(sd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
puts("Not binded");
return 1;
} else {
puts("Binded");
}
len = sizeof(cliaddr);
recvfrom(sd, buff, 1024, 0, (struct sockaddr *)&cliaddr, &len);
printf("%s\n", buff);
FILE *fp = fopen(buff, "r");
if (fp == NULL) {
printf("File does not exist \n");
return 1;
}
while (1) {
bytes_read = read(fp, content, sizeof(content));
if (bytes_read == 0)
break;
sendto(sd, content, sizeof(content), 0, (struct sockaddr*)&cliaddr, len);
bzero(content, 200);
}
strcpy(content, "end");
sendto(sd, content, sizeof(content), 0, (struct sockaddr*)&cliaddr, len);
return 0;
}
Client side:
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netinet/in.h>
#include <sys/types.h>
int main() {
int count = 0;
char buff[20], output[20];
char file_buffer[200];
int sockfd, connfd, len;
struct sockaddr_in servaddr, cliaddr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
puts("socket not created in client");
return 1;
} else {
puts("socket created in client");
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY; // ANY address or use specific address
servaddr.sin_port = htons(7802); // Port address
puts("Type your UDP client message");
scanf("%s", buff);
puts("enter the name of new file to be saved");
scanf("%s", output);
// send msg to server
sendto(sockfd, buff, strlen(buff) + 1, 0,
(struct sockaddr *)&servaddr, sizeof(struct sockaddr));
count++;
printf("%d\n", count);
FILE *fp = fopen(output, "a");
if (fp == NULL) {
puts("error in file handling");
return 1;
}
recvfrom(sockfd, file_buffer, sizeof(file_buffer), 0, NULL, NULL);
while (1) {
if (strcmp(file_buffer, "end") == 0)
break;
printf("%s", file_buffer);
write(fp, file_buffer, strlen(file_buffer));
bzero(file_buffer, 200);
recvfrom(sockfd, file_buffer, sizeof(file_buffer), 0, NULL, NULL);
}
puts("completed");
return 0;
}
Probably you should use the fread() function instead of read() on server side.
Functions fopen() and read() are parts of different interfaces:
Family read() -> open, close, read, write, ioctl (system calls)
Family fread() -> fopen, fclose, fread, fwrite, fcntl (standard library)
I can suppose that file descriptor given by fopen() is not exactly the same as read() expects.
The same on client side. You use the fopen() to open file and write() to write. Please use the fwrite()

c socket prog - select() problems

I'm new to network programming. I have to write a simple client/server program in C. The server will listen for a connection and the client will connect to the server, send a message, and receive an echo back from the client. We have to update this using select() to handle connections to multiple clients at the same time from the server process. I tried to implement select() on the client side like instructed,but I think I'm having an infinite loop on the client side in if(FD_ISSET(clientSockfd, &readfds)) part.
//client1.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <resolv.h>
#include <arpa/inet.h>
const int BUF_SIZE = 512;
int main(int argc, char *argv[]) {
char buf[BUF_SIZE], buf2[BUF_SIZE];
char *msg;
struct sockaddr_in serverInfo;
int clientSockfd, errorCheck, readVal, numfd;
struct hostent *hostName;
fd_set readfds;
//make sure user entered correct arguments when starting client
if(argc != 3)
{
printf("error: must enter 'programName portNumber hostname'\n");
exit(errno);
}
//create socket and error check socket() call
clientSockfd=socket(AF_INET, SOCK_STREAM, 0);
if (clientSockfd == -1)
{
perror("error creating socket");
exit(errno);
}
//assign sockaddr_in info for RemoteAddr
bzero(&serverInfo, sizeof(serverInfo));
serverInfo.sin_family=AF_INET;
hostName=gethostbyname(argv[2]);
if(hostName == NULL)
{
herror("Error when calling gethostbyname()");
exit(errno);
}
memcpy((unsigned char *) &serverInfo.sin_addr, (unsigned char *)hostName->h_addr, hostName->h_length); //copy IP address to be used
serverInfo.sin_port=htons(atoi(argv[1])); //port number to be used, given in command line, must be converted to network byte order
//connect to server side
if(connect(clientSockfd, (struct sockaddr *)&serverInfo, sizeof(serverInfo)) == -1)
{
perror("error when connecting to server");
exit(errno);
}
while(1)
{
FD_ZERO(&readfds); //zero out set
FD_SET(fileno(stdin), &readfds);
FD_SET(clientSockfd, &readfds);
int maxfd = fileno(stdin);
if(maxfd < clientSockfd) maxfd = clientSockfd;
numfd = select(maxfd, &readfds, 0, 0, 0); //call select()
if(numfd > 0)
{
if(FD_ISSET(clientSockfd, &readfds))
{
//make sure buf is empty so it doesnt print extra chars
bzero(buf2, BUF_SIZE);
read(clientSockfd, buf2, BUF_SIZE);
}
if(FD_ISSET(fileno(stdin), &readfds))
{
bzero(buf, BUF_SIZE);
fgets(buf, BUF_SIZE-1, stdin);
printf("echo from server: %s\n", buf);
errorCheck = write(clientSockfd, buf, strlen(buf)+1);
if(errorCheck == -1)
{
perror("error writing");
}
}
}
else if(numfd == 0)
{
perror("Error using select()\n");
exit(0);
}
else
printf("no data\n");
}
//close connection to server
errorCheck = close(clientSockfd);
if(errorCheck == -1)
{
perror("Error closing connection.");
exit(errno);
}
return 0;
}
here is the server..
//server.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/socket.h>
#include <resolv.h>
#include <arpa/inet.h>
#include <errno.h>
const int ASSIGNED_PORT = 17000;
const int BUF_SIZE = 512;
int main() {
int serverfd, clientfd;
struct sockaddr_in serverSock; //NOTE: a pointer to sockaddr_in can be cast to a pointer to
// a struct sockaddr - useful for connect()
char buf[BUF_SIZE];
int errorCheck, msgLength;
//create socket with error checking (-1 ret on error)
serverfd = socket(AF_INET, SOCK_STREAM, 0);
if(serverfd < 0 )
{
perror("socket failed.");
exit(errno);
}
//assign sockaddr_in info for server
bzero(&serverSock, sizeof(serverSock)); //set to all 0's
serverSock.sin_family = AF_INET;
serverSock.sin_addr.s_addr = htonl(INADDR_ANY);
serverSock.sin_port = htons(ASSIGNED_PORT);
//bind a name to the socket with error checking (0 ret on success)
errorCheck = bind(serverfd, (struct sockaddr*)&serverSock, sizeof(serverSock));
if(errorCheck < 0 )
{
perror("bind failed.");
exit(errno);
}
//listen for connections with error checking (0 ret on success)
errorCheck = listen(serverfd, 10);
if(errorCheck < 0 )
{
perror("listen failed.");
exit(errno);
}
printf("Listening for connections. Enter CNTRL-c to kill server.\n");
//create infinite loop to accept, read, write, and close connections with error hecking
while(1)
{
//accept the connection from the client
clientfd = accept(serverfd, 0, 0);
if(clientfd == -1)
{
perror("error accepting connection.");
exit(errno);
}
//read data from the client
bzero(buf, BUF_SIZE);
msgLength = read(clientfd, buf, BUF_SIZE-1);
if(msgLength == -1)
{
perror("error reading from client");
close(clientfd);
close(serverfd);
exit(errno);
}
if(buf[0] '\0')
{
printf("connection closing");
exit(0);
}
//print what the client sent
printf("Message from client: %s\n", buf);
//echo back what the client sent
errorCheck = write(clientfd, buf, strlen(buf)+1);
if(errorCheck == -1 )
{
perror("error writing to client");
exit(errno);
}
//close the connection
errorCheck = close(clientfd);
if(errorCheck == -1)
{
perror("error closing connection");
exit(errno);
}
}
errorCheck = close(serverfd);
if(errorCheck==-1)
{
perror("error closing server, exiting program now");
sleep(6);
exit(errno);
}
return 0;
}
I think the problem (or at least, a problem) in your client-side code is that you are passing the magic number 32 to select(). That's incorrect. What you should be passing is the maximum of all of the socket numbers, plus one. For example, something like this:
int maxfd = fileno(stdin);
if (maxfd < clientSockFD) maxfd = clientSockFD;
// further maximizations for other sockets would go here, if you had any other sockets...
numfd = select(maxfd+1, &readfds, 0, 0, 0);
You need to use select() on the server side to handle multiple clients, not on the client side.

Resources