i have a few questions.
one, if i use read like this
Code:
read(file_des, buffer, sizeof(buffer)-1);
that will read from the buffer, x characters, and pad the rest with null terminators right?? Will that affect anything?
second, what is the difference if i run the client this way: ./client 0.0.0.0 1234
as to: ./client 127.0.0.1 1234
third, i have a select server that MK27 helped me with. And i am compiling and running it, and i get the following. run server with ./select_server 1234 and it waits. I run the client, ./client 127.0.0.1 1234
the client says,
made connection to blah blah blah
Message:
i type in a message and server says,
Server: got message: hey
Server: got message: %
then just hangs. i ran strace on the server and here are the results:
Code:
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\220o\1\0004\0\0\0"..., 512) = 512
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
bind(3, {sa_family=AF_INET, sin_port=htons(1234), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
listen(3, 10) = 0
select(1024, [3], NULL, NULL, NULL) = 1 (in [3])
accept(3, {sa_family=AF_INET, sin_port=htons(52858), sin_addr=inet_addr("127.0.0.1")}, [16]) = 4
read(4, "hey\n\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 1023) = 1023
write(1, "Server: got message: hey\n", 25Server: got message: hey
) = 25
write(1, "\n", 1
) = 1
select(1024, [3 4], NULL, NULL, NULL) = 1 (in [4])
read(4, "K\201", 1023) = 2
write(1, "Server: got message: K\201\n", 24Server: got message: K�
) = 24
select(1024, [4], NULL, NULL, NULL) = 1 (in [4])
read(4, "", 1023) = 0
write(2, "Exiting \n", 9Exiting
) = 9
Code:
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
int read_from_client(int file_des) {
char buffer[1024];
int read_var;
int i = sizeof(buffer)-1; if(i > 0) bzero(buffer, sizeof(buffer));
read_var = read(file_des, buffer, sizeof(buffer)-1);
if(read_var < 0) {
fprintf(stderr, "READ() error \n");
exit(1);
} else if(read_var == 0) {
fprintf(stderr, "Exiting \n");
exit(1);
}
else {
fprintf (stdout, "Server: got message: %s\n", buffer);
return 0;
}
}
int main(int argc, char *argv[]) {
int sockfd, newsockfd, portno, i;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
fd_set read_fds;
int yes = 1;
if (argc < 2) fprintf(stderr, "No port provided ");
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
fprintf(stderr, "Error creating socket SOCKET()\n");
exit(1);
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
fprintf(stderr, "Address already in use SETSOCKOPT()\n");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(portno);
if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
fprintf(stderr, "Error binding to socket BIND()\n");
exit(1);
}
listen(sockfd, 10);
clilen = sizeof(cli_addr);
FD_ZERO(&read_fds);
FD_SET(sockfd, &read_fds);
for( ; ; ) { /*1rst for loop*/
i = select(FD_SETSIZE, &read_fds, NULL, NULL, NULL);
if (i < 0) { /*1rst if statement*/
fprintf(stderr, "Select server error SELECT() %s\n", strerror(errno));
exit(1);
}
for (i = 0; i < 20; ++i) { // should be high enough
if(FD_ISSET(i, &read_fds)) {
if(i == sockfd) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) {
fprintf(stderr, "Error accepting new connection ACCEPT() %s\n", strerror(errno));
exit(1);
}
FD_SET(newsockfd, &read_fds);
} else {
if(read_from_client (i) < 0) {
close(i);
FD_CLR(i, &read_fds);
}
}
}
}
}
}
thanks in advanced.