Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8924473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:25:36+00:00 2026-06-15T07:25:36+00:00

Here is my problem: when I start my server and try to connect to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:25:37+00:00Added an answer on June 15, 2026 at 7:25 am

    You are losing the values you pass to serv, because in the context of the setupServer() you have a local copy of the value(s) of serverSettings and your modifications are not visible outside the function. To preserve them after setupServer finishes, you need to use a pointer:

    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
    }
    

    then call it as follows:

    serverSocket = setupServer(PORT, &serverSettings);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my situation: I'm writing a chat client to connect to a chat server.
Ok here is my problem : Before i start the description, let me to
i make a simple TCP client/server in C# and i have the problem. When
I try to connect a server though SSH using Ruby, but I have an
Currently I have this problem. Client downloads from server successfully only the 1st time.
I implemented simple server-client chat in Java. Here the source for the server: public
I can use mysql from terminal to query data and I try to connect
I am building a Client server app in java ,here is my code Client
I have a PC server and an android client; my android client start a
I thought to share this relatively smart problem with everyone here. I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.