send() function is never called in server C - c

I have a server program here that checks the input sent from the client and returns a message back to the client wether the password is right or wrong .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Practical.h"
#include <unistd.h>
#include <sys/stat.h>
#define BUFSIZE 200
int main(int argc, char* argv[])
{
int numBytes = 0;
int MAXPENDING = 1;
char recvbuffer[BUFSIZE], sendbuffer[BUFSIZE];
char inp_user[BUFSIZE];
char inp_pass[BUFSIZE];
char pass[BUFSIZE] = "pass";
char user[BUFSIZE] = "admin";
int attemptsLeft = 3;
if (argc != 2)
DieWithUserMessage("Parameter(s)", "<Server Port>");
in_port_t servPort = atoi(argv[1]);
int servSock;
if ((servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithSystemMessage("socket() failed");
struct sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(servPort);
if (bind(servSock, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0)
DieWithSystemMessage("bind() failed");
//accept incoming connection requests to the server socket
//MAXPENDING is the maximum number of connections
//that should be queued for this socket
if (listen(servSock, MAXPENDING) < 0)
DieWithSystemMessage("listen() failed");
for (;;)
{
//declare client address structure
struct sockaddr_in clntAddr;
socklen_t clntAddrLen = sizeof(clntAddr);
//return new file descriptor referring
//to the client socket , all read/write operations will be
//performed on this socket
int clntSock = accept(servSock, (struct sockaddr *) &clntAddr, &clntAddrLen);
//check if the client socket has not been created
if (clntSock < 0)
DieWithSystemMessage("accept() failed");
//retrieve the data sent by the client
while ((numBytes = recv(clntSock, recvbuffer, (BUFSIZE-1), 0)) > 0) {
recvbuffer[numBytes] = '\0';
fputs(recvbuffer, stdout);
// if(strstr(recvbuffer, "\r\n\r\n") > 0)
// break;
}
//check if data has not been received from the client
if (numBytes < 0)
DieWithSystemMessage("recv() failed");
//read username and password into the server's buffers
sscanf(recvbuffer, "%s %s", inp_user, inp_pass);
//check if the entered username and pasword is correct
if( (strcmp(user, inp_user) == 0)
&&
(strcmp(pass, inp_pass) == 0)
)
{
snprintf(sendbuffer, sizeof(sendbuffer), "%s", "PROCEED\r\n");
}
else //the credentials entered were incorrect
{
attemptsLeft--;
//check if user has no attempts left
if(attemptsLeft == 0) {
snprintf(sendbuffer, sizeof(sendbuffer), "%s", "DENIED\r\n" );
} else {
snprintf(sendbuffer, sizeof(sendbuffer), "%s", "NONO\r\n");
// "You have %d attempt(s) left", attemptsLeft);
}
}
printf("The send message contains %s \n\n", sendbuffer);
printf("brefore sending\n");
ssize_t numBytesSent = send(clntSock, sendbuffer, strlen(sendbuffer), 0);
printf("after sending\n");
if (numBytesSent < 0)
DieWithSystemMessage("send() failed");
printf("NOBYTES: %d", numBytes);
close(clntSock);
}
}
The message contains prints successfully meaning a message is created for the user however the send() function is never called and the client never receives this message. Anyone know why this would happen?

Related

How do I use both writefds and readfds for multiple client server communication program in C language?

I am currently trying to create a multiple client-server which enables server to perform both read and write functions in C language. I am able to read the data from the client using readfds and putting it as parameter in SELECT. When I added in writefds, the client fails to connect to the server. I am not sure what is the issue behind it, whether this is the correct method to transmit and receive data
This is the code for my server
#include <stdio.h>
#include <string.h> //strlen
#include <stdlib.h>
#include <errno.h>
#include <unistd.h> //close
#include <arpa/inet.h> //close
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO macros
#define TRUE 1
#define FALSE 0
#define PORT 8080
int main(int argc , char *argv[])
{
int opt = TRUE;
int master_socket , addrlen , new_socket , client_socket[30] ,
max_clients = 2 , activity, i , valread , sd;
int max_sd;
struct sockaddr_in address;
char buffer[1025]; //data buffer of 1K
fd_set readfds;
fd_set writefds;
char *message = "Welcome to Server\r\n";
//initialise all client_socket[] to 0 so not checked
for (i = 0; i < max_clients; i++)
{
//client_socket[i] = 0;
}
//create a master socket
if( (master_socket = socket(AF_INET , SOCK_STREAM , 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
if( setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,
sizeof(opt)) < 0 )
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
//type of socket created
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
//bind the socket to localhost port 8888
if (bind(master_socket, (struct sockaddr *)&address, sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
printf("Listener on port %d \n", PORT);
if (listen(master_socket, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
addrlen = sizeof(address);
puts("Waiting for connections ...");
for(;;)
{
//clear the socket set
FD_ZERO(&readfds);
FD_ZERO(&writefds);
//add master socket to set
FD_SET(master_socket, &readfds);
FD_SET(master_socket, &writefds);
max_sd = master_socket;
//add child sockets to set
for ( i = 0 ; i < max_clients ; i++)
{
//socket descriptor
sd = client_socket[i];
//if valid socket descriptor then add to read list
if(sd > 0)
FD_SET( sd , &readfds);
//highest file descriptor number, need it for the select function
if(sd > max_sd)
max_sd = sd;
}
//wait for an activity on one of the sockets , timeout is NULL ,
//so wait indefinitely
activity = select( max_sd + 1 , &readfds , &writefds, NULL , NULL);
if ((activity < 0) && (errno!=EINTR))
{
printf("select error");
}
if (FD_ISSET(master_socket, &readfds))
{
if ((new_socket = accept(master_socket,
(struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
else {
printf("Connected to socket \n");
}
bzero(buffer, 1025);
int n=0;
read(new_socket, buffer, sizeof(buffer));
printf("From client: %s\t To client: ", buffer);
bzero(buffer, 1025);
//add new socket to array of sockets
for (i = 0; i < max_clients; i++)
{
//if position is empty
if( client_socket[i] == 0 )
{
client_socket[i] = new_socket;
printf("Adding to list of sockets as %d\n" , i+1);
}
}
}
else if(FD_ISSET(master_socket, &readfds)) {
printf("From client: %s\t To client : ", buffer);
bzero(buffer, 1025);
// copy server message in the buffer
while ((buffer[n++] = getchar()) != '\n')
;
write(new_socket, buffer, sizeof(buffer));
}
else{
//else its some IO operation on some other socket
for (i = 0; i < max_clients; i++)
{
sd = client_socket[i];
if (FD_ISSET( sd , &readfds))
{
//Check if it was for closing , and also read the
//incoming message
if ((valread = read( sd , buffer, 1024)) == 0)
{
close( sd );
client_socket[i] = 0;
}
//Echo back the message that came in
else
{
}
}
}
}//else
}
return 0;
}
This is the code for the client
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#define MAX 1000
#define PORT 8080
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
int firstConnect = 1;
for (;;)
{
bzero(buff, sizeof(buff));
n = 0;
if(firstConnect==1) {
printf("Enter the string : ");
firstConnect = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
}
bzero(buff, sizeof(buff));
read(sockfd, buff, sizeof(buff));
printf("From Server: %s\t To Server : ", buff);
while ((buff[n++] = getchar()) != '\n');
write(sockfd, buff, sizeof(buff));
if ((strncmp(buff, "exit", 4)) == 0)
{
printf("Client Exit...\n");
break;
}
}
}
int main()
{
printf("CLIENT");
int sockfd, connfd;
struct sockaddr_in servaddr, cli;
// socket create and varification
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
printf("socket creation failed...\n");
exit(0);
}
else
printf("Socket successfully created..\n");
bzero(&servaddr, sizeof(servaddr));
// assign IP, PORT
servaddr.sin_family = AF_INET;
// servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_addr.s_addr = inet_addr("127.8.1.0");
servaddr.sin_port = htons(PORT);
// connect the client socket to server socket
if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) != 0)
{
printf("connection with the server failed...\n");
exit(0);
}
else
printf("connected to the server..\n");
// function for chat
func(sockfd);
// close the socket
close(sockfd);
}
As side node, it appears that you expect read to zero-terminate received data. That is not the case. The length of received data is in the return value of read, it must not be ignored.
It also appears that you expect write to find the end of the string you are sending. That doesn't happen. You must specify the exact length of the data you are sending, rather than the length of the buffer that contains the data.
send/write and recv/read functions work on binary data rather than zero-terminated strings and can send/receive less than the specified size or may return an error. Learn how to use these functions correctly by not ignoring the return value and handling errors and partial reads and writes.

Socket recv() function get null message

I have a problem about the recv() function.
When I sent the first message(ex: hi) to server first, the server would send back the same message to client.
And I sent again(ex: hi2), I would get a null message.
But I kept sending message,maybe I sent two times again. I got the second message I sent.
I want the result is client send to server and client receive the right message immediately.
How can I solve this problem?
my client:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc , char *argv[])
{
int sockFD = 0;
sockFD = socket(AF_INET , SOCK_STREAM , 0);
if (sockFD == -1){
printf("Fail to create a socket.");
}
struct sockaddr_in info;
bzero(&info,sizeof(info));
info.sin_family = PF_INET;
info.sin_addr.s_addr = inet_addr("127.0.0.1");
info.sin_port = htons(8080);
if(connect(sockFD,(struct sockaddr *)&info,sizeof(info)) == -1){
perror("connect()");
}
char receiveMessage[100] = {};
memset(receiveMessage, 0, sizeof(receiveMessage));
char* input;
while(scanf("%s", input) == 1){
if(strcmp("exit", input) == 0){
break;
}
if(send(sockFD, input, sizeof(input), 0) < 0){
perror("send()");
}
if(recv(sockFD, receiveMessage, sizeof(receiveMessage), 0) < 0){
perror("recv()");
}
printf("Server: %s\n",receiveMessage);
}
printf("close Socket\n");
close(sockFD);
return 0;
}
my server:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main()
{
int sockFD = 0, clientSockFD= 0;
char inputBuffer[256] = {};
//char outputBuffer[256] = {};
fd_set masterFD;
fd_set readFD;
int fdMax;
// create socket
sockFD = socket(AF_INET, SOCK_STREAM, 0);
if(sockFD == -1){
perror("socket()");
exit(0);
}
struct sockaddr_in serverInfo, clientInfo;
int addrLen = sizeof(clientInfo);
bzero(&serverInfo, sizeof(serverInfo));
serverInfo.sin_family = PF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htons(8080);
// bind port
if(bind(sockFD, (struct sockaddr *)&serverInfo, sizeof(serverInfo))){
perror("bind()");
}
// listen
if(listen(sockFD, 10)){
perror("listen()");
}
FD_SET(sockFD, &masterFD);
fdMax = sockFD;
while(1){
readFD = masterFD;
if(select(fdMax + 1, &readFD, NULL, NULL, NULL) == -1){
perror("select()");
exit(0);
}
for(int i = 0; i <= fdMax; i++){
if(FD_ISSET(i, &readFD)){
if(i == sockFD){
if((clientSockFD = accept(sockFD, (struct sockaddr *)&clientInfo, &addrLen)) < 0){
perror("accept()");
}
FD_SET(clientSockFD, &masterFD);
if(clientSockFD > fdMax){
fdMax = clientSockFD;
}
printf("selectserver: new connection\n");
}
else{
int recvStatus = recv(i, inputBuffer, sizeof(inputBuffer), 0);
if(recvStatus <= 0){
if(recvStatus < 0){perror("recv()");}
else if(recvStatus = 0){printf("colse connected");}
close(i);
FD_CLR(i, &masterFD);
}
else{
printf("client: %s\n", inputBuffer);
send(i, inputBuffer, sizeof(inputBuffer), 0);
}
}
}
}
}
return 0;
}
Change
send(i, inputBuffer, sizeof(inputBuffer), 0);
Into:
send(i, inputBuffer,recvStatus , 0);
The cause: you were sending a complete buffer, containing only a few intended characters, but padded (upto size=256) with NULs, (or whatever garbage is the buffer from previous calls))
recv() and read() return the number of characters received. (or 0 on EOF,or -1 on errors (some of which could be handled, for instance: EAGAIN, EINTR))

client server communication is not happening. The client code is not displaying after send function

I don't know whether the problem is with the client or with the server or both.This is my first client-server socket programming code. But this is not working as expected. The code which I referenced is working well although.
When the code runs, the client and server should both exchange 2 messages, but they are not doing so. The server is displaying "Listening" which is right as expected but when I run the client code, Nothing happens, It just displays nothing.
This is the client code
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
int main() {
struct sockaddr_in mysocket, servsocket;
int err;
char buf[256];
//CREATING SOCKET
int socketstatus = socket(AF_INET, SOCK_STREAM, 0);
printf("%d\n", socketstatus);
if(socketstatus < 0){
printf("socket failed\n");
scanf("%d", &err);
return 0;
}
bzero((char *) &mysocket, sizeof(mysocket));
mysocket.sin_family = AF_INET;
mysocket.sin_addr.s_addr = inet_addr("127.0.0.2");
int port = 5674;
mysocket.sin_port = htons(port);
//CONNECT
int connectstatus = connect(socketstatus, (struct sockaddr *) &servsocket, sizeof(servsocket));
if(connectstatus < 0){
printf("Connect failed\n");
scanf("%d", &err);
return 0;
}
//SEND
bzero(buf, 256);
strcpy(buf, "Message sent by client");
int sendstatus = send(socketstatus, buf, 256, 0);
printf("2\n"); //This is not being displayed
if(sendstatus < 0){
printf("Client send failed\n");
return 0;
}
printf("Reached for receiving\n");
//RECEIVE
bzero(buf, 256);
int recvstatus = recv(socketstatus, buf, 256, 0);
if(recvstatus < 0){
printf("Client RECEIVE failed\n");
scanf("%d", &err);
return 0;
}
printf("The message client got from server is, %s \n",buf );
scanf("%d", &err);
printf("Bye");
}
And this is the server code:
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
int main() {
int err;
struct sockaddr_in servsocket, clientsocket;
char sendmessage[256];
//CREATING SOCKET
int mysocket = socket(AF_INET, SOCK_STREAM, 0);
if(mysocket < 0){
printf("socket creation failed\n");
scanf("%d", &err);
return 0;
}
bzero((char*) &servsocket, sizeof(servsocket)); //initiazlizing servsocket with null
servsocket.sin_family = AF_INET;
servsocket.sin_addr.s_addr = inet_addr("127.0.0.2");
int port = 5674;
servsocket.sin_port = htons(port);
//BINDING
int bindstatus = bind(mysocket, (struct sockaddr*) &servsocket, sizeof(servsocket));
if(bindstatus < 0){
printf("Socket bind failed\n");
scanf("%d", &err);
return 1;
}
//LISTENING
int listenstatus = listen(mysocket, 5);
if(listenstatus < 0){
scanf("%d", &err);
return 2;
}
else
printf("LISTENING....\n");
//ACCEPTING
int clientsize = sizeof(clientsocket);
int acceptstatus = accept(mysocket, (struct sockaddr*) &clientsocket, &clientsize);
if(acceptstatus < 0){
printf("Accept failed");
scanf("%d", &err);
return 3;
}
char buf[256];
bzero(buf, 256);
//RECEIVING
int recvstatus = recv(acceptstatus, buf, 256, 0);
if(buf < 0){
printf("Error:Nothing read");
scanf("%d", &err);
return 4;
}
printf("I received this message, %s \n", buf);
printf("NOW I WILL SEND\n");
//SENDING
bzero(sendmessage, 256);
strcpy(sendmessage, "Message sent by server");
int sendstatus = send(acceptstatus, sendmessage, sizeof(sendmessage), 0);
if(sendstatus < 0){
printf("Error sending\n");
scanf("%d", &err);
return 5;
}
return 0;
}
In the client code, you initialize mysocket but pass serversocket to connect uninitialized.
You should be setting the fields of serversocket instead of mysocket.
You want to connect to the server socket inside your client (that would be serversocket in your code, not mysocket):
bzero((char *) &servsocket, sizeof(servsocket));
servsocket.sin_family = AF_INET;
servsocket.sin_addr.s_addr = inet_addr("127.0.0.2");
int port = 5674;
servsocket.sin_port = htons(port);
Then, I think you want your inet address to be 127.0.0.1 (what is typically default localhost address), not 127.0.0.2.
The code was "working" because you were passing a correct socket descriptor (socketstatus) to it is not correctly connected to the endpoint, so it fails on the send() call.

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.

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