Here is my problem: when I start my server and try to connect to it with a client, the client cannot find the endpoint. I’m almost positive it isn’t the client because I can connect to another server with the same client. Yes I am using the same port number for the client and server and yes I am using “127.0.0.1” for the address. I can start a different server on the same port and connect to it with the client. My client code is posted at the very bottom. When I run my server it sits where its suppose to, on the socket = acceptSocket(…) line, but my client can’t connect to it. Does anyone see the problem? Thanks for reading!
Here is the output I get for the server:
Creating a new server…
waiting for a client…
and here is the output for the client:
Starting up a new client connection….
PORT: 8081
ADDRESS: 127.0.0.1
Success? false
Connect failed, try restarting server.
Here is my server.c code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "server.h"
//returns a setup server socket
int setupServer(int port, struct sockaddr_in serv)
{
memset(&serv, 0, sizeof(serv)); /* zero the struct before filling the fields */
serv.sin_family = AF_INET; /* set the type of connection to TCP/IP */
serv.sin_addr.s_addr = INADDR_ANY; /* set our address to any interface */
serv.sin_port = htons(port); /* set the server port number */
return socket(AF_INET, SOCK_STREAM, 0); // returns a setup server socket
}
int startServer(int serverSocket, struct sockaddr_in serv)
{
bind(serverSocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
return listen(serverSocket, 1);
}
int acceptSocket(int serverSocket, struct sockaddr_in serv, struct sockaddr_in client)
{
unsigned int socksize = sizeof(struct sockaddr_in); // how many memory blocks is socket
printf("%s %d","Socket sze:::", socksize);
return accept(serverSocket, (struct sockaddr *)&client, &socksize);
}
void closeServer(int socket)
{
shutdown(socket, SHUT_RDWR);
close(socket);
}
and here is the header for server.c (server.h):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#ifndef ServerTools_server_h
#define ServerTools_server_h
int setupServer(int port, struct sockaddr_in serv);
int startServer(int serverSocket, struct sockaddr_in serv);
int acceptSocket(int serverSocket, struct sockaddr_in serv, struct sockaddr_in client);
void closeServer(int socket);
#endif
here is the main.c that I use to create the server and start it:
#include <stdio.h>
#include "server.h"
#define PORT 8081
void println(char* c);
int serverSocket;
struct sockaddr_in serverSettings;
int main(int argc, const char * argv[])
{
println("Creating a new server...");
serverSocket = setupServer(PORT, serverSettings);
startServer(serverSocket, serverSettings);
println("waiting for a client...");
struct sockaddr_in client;
int socket = acceptSocket(serverSocket, serverSettings, client);
printf("%s %d", "socket accepted: ", socket);
return 0;
}
void println(char* c)
{
printf("%s%s", c, "\n");
}
My client code, written in Java:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client implements Runnable
{
public Client()
{
System.out.println("Starting up a new client connection....");
System.out.println("PORT: " + PORT);
System.out.println("ADDRESS: " + ADDRESS);
boolean success = connect();
System.out.println("Success? " + success);
if(!success)
{
System.out.println("Connect failed, try restarting server.");
System.exit(0);
}
System.out.println("Connection is setup, listening for data...");
running = true;
inputThread = new Thread(this);
inputThread.start();
consoleThread = new Thread(new Runnable()
{
public void run()
{
consoleInput = new BufferedReader(new InputStreamReader(System.in));
while(running)
{
try
{
String message = "";
while((message = consoleInput.readLine()) != null)
{
Client.this.sendMessage(message);
}
}catch(Exception e){}
}
}
});
consoleThread.start();
}
public boolean connect()
{
try
{
socket = new Socket(ADDRESS, PORT);
writer = new PrintWriter(socket.getOutputStream());
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
return true;
}catch(Exception e){}
return false;
}
public void run()
{
while(running)
{
try
{
String message = "";
while((message = reader.readLine()) != null)
{
System.out.println("Message recieved: " + message);
}
}catch(Exception e){}
}
}
public void sendMessage(String message)
{
writer.println(message);
writer.flush();
if(!writer.checkError())
System.out.println("Message sent: " + message);
else
System.out.println("Message not sent.");
}
private Socket socket;
private PrintWriter writer;
private BufferedReader reader;
private BufferedReader consoleInput;
private boolean running;
private Thread consoleThread;
private Thread inputThread;
public static final int PORT = 8081;
public static final String ADDRESS = "127.0.0.1";
public static void main(String args[])
{
new Client();
}
}
from telnet:
Trying 127.0.0.1…
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host
exception from java:
java.net.ConnectException: Connection refused
You are losing the values you pass to
serv, because in the context of thesetupServer()you have a local copy of the value(s) ofserverSettingsand your modifications are not visible outside the function. To preserve them aftersetupServerfinishes, you need to use a pointer:then call it as follows: