2

everybody. Hope you can help me with this one: I have two threads, which are tasked with handling connections from a client.

This is my code

ServerSocket loginSocket = new ServerSocket(8000);
ServerSocket fileSocket = new ServerSocket(7000);

while (running) {
  new LoginThread(loginSocket.accept(),loginInormation).start();
  new LoaderThread(fileSocket.accept()).start();
}

When I try to connect to the loginSocket two times, the server will block and stop working, blocking the client, but this doesn't happen if I delete this:

new LoginThread(loginSocket.accept(),loginInormation).start();

I'm not getting any error messages, so why is this happening and how can I fix this?

Mr. SoUndso
  • 83
  • 2
  • 7
  • `loginSocket.accept()` is probably waiting for a connection? – Steve Smith Jun 13 '17 at 14:21
  • I looked into this : https://stackoverflow.com/questions/10131377/socket-programming-multiple-client-to-one-server and I do not see where you are wrong. I think we need more code. For instance, what is in `LoginThread` and `LoaderThread`. Also, are you sure you are not catching exceptions silently? – Demogorii Jun 13 '17 at 14:21

3 Answers3

0

The accept() method is a blocking method, which means that your program won't continue until a connection is made with loginSocket().

When you're creating your LoginThread, the program waits a connection to set the first parameter of your object, and it will not continue the execution until a connection is made.

Kaiserbogey
  • 191
  • 13
0

The line new LoginThread(loginSocket.accept(),loginInormation).start(); contains the method call loginSocket.accept(), which will be called before this thread is created. This method call will block until a client logs in. (In addition, the second thread will be blocked by fileSocket.accept()).

As for a solution, I would move the accept() calls to inside each of the Threads. You will need to pass the sockets to the threads for them to do this.

Steve Smith
  • 2,244
  • 2
  • 18
  • 22
0

Start fileSocket and login socket in different threads

package com.ca.training.task.app;

import java.io.IOException;
import java.net.ServerSocket;

public class App {

    public void execute() {
        LoginRunnable loginRunnable = new LoginRunnable();
        loginRunnable.setLoginInformation(new Object());//Login information

        FileRunnable fileRunnable = new FileRunnable();//Data for loaded runnable.
        fileRunnable.setParams(new Object());

        startLoginThread(loginRunnable);
        startFileThread(fileRunnable);
    }

    private static void startLoginThread(LoginRunnable loginRunnable) {
        Thread loginThread = new Thread(loginRunnable);
        loginThread.start();
    }

    private static void startFileThread(FileRunnable fileRunnable) {
        Thread loadedThread = new Thread(fileRunnable);
        loadedThread.start();
    }

    class LoginRunnable implements Runnable {

        private Object loginInformation;

        @Override
        public void run() {
            try {
                ServerSocket loginSocket = new ServerSocket(8000);
                loginSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public Object getLoginInformation() {
            return loginInformation;
        }

        public void setLoginInformation(Object loginInformation) {
            this.loginInformation = loginInformation;
        }
    }

    class FileRunnable implements Runnable {

        private Object params;

        @Override
        public void run() {
            try {
                ServerSocket fileSocket = new ServerSocket(7000);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public Object getParams() {
            return params;
        }

        public void setParams(Object params) {
            this.params = params;
        }
    }
}
Yan Khonski
  • 12,225
  • 15
  • 76
  • 114