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

  • Home
  • SEARCH
  • 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 8750355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:51:05+00:00 2026-06-13T12:51:05+00:00

i try creating client-server app using socket. i already succeed doing that with the

  • 0

i try creating client-server app using socket.
i already succeed doing that with the AVD and run both server and client on my pc machine.
but when i try make it work in same Wifi network on my device, the app just crash.

yes, i’m using seperate thread for the connection
and already added the use of Internet to the manifest.

here is some code…

the client thread:

package com.mainlauncher;

import java.io.*;
import java.net.*;

public class ConnectionThread extends Thread {

private static final int SERVERPORT = 7777;
private static final String SERVERADDRESS = "My-PC";
private Socket socket;
private DataInputStream in;
private DataOutputStream out;

@Override
public void run() {
    super.run();
    open();
    close();
}

void open(){
    try{
        socket = new Socket(SERVERADDRESS,SERVERPORT);
        in = new DataInputStream(socket.getInputStream());
        out = new DataOutputStream(socket.getOutputStream());
    }
    catch(IOException e){}
}

void close(){
    try {
        if(in!=null)
            in.close();
        if(out!=null)
            out.close();
        if(socket!=null)
            socket.close();
    } 
    catch (IOException e) {}
    socket=null;

}


}

the Server side main:

import java.io.IOException;
import java.net.*;


public class Main {

public static void main(String[] args) {
    int port = 7777;
    new Main().handleClients(port);
}

private void handleClients(int port) {
    ServerSocket serverSocket = null;
    try{
        serverSocket = new ServerSocket(port);
        System.out.println("Server is ready...");
        for(int i=1; ; i++){
            Socket socket = serverSocket.accept();
            ServerThread thread = new ServerThread(i,socket);
            System.out.println(i + " Connected");
            thread.run();
        }
    }
    catch (Exception e){
        System.err.println(e.getMessage());
    }
    finally{
        if(serverSocket != null){
            try{ serverSocket.close(); }
            catch(IOException x){}
        }

    }


}

}

and the ServerThread:

import java.io.*;
import java.net.*;


public class ServerThread extends Thread {

private int serverIndex;
private Socket socket;
private DataOutputStream out;
private DataInputStream in;

public ServerThread (int serverIndex, Socket socket){
    this.serverIndex = serverIndex;
    this.socket = socket;
}

@Override
public void run() {
    super.run();

    try {
        in = new DataInputStream(socket.getInputStream());
        out = new DataOutputStream(socket.getOutputStream());
    } catch (IOException e) {
        System.out.println(serverIndex + " Disconnected");
    }

    finally{
        try {
            in.close();
            out.close();
            socket.close();
        } catch (IOException e) {}
    }
}
}

i tried looking for answers here \ google etc…
nothing helped.
there is no firewall or anything to block the incoming connection on my pc.

any ideas anyone?

thanks,
Lioz

  • 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-13T12:51:05+00:00Added an answer on June 13, 2026 at 12:51 pm

    This won’t immediately solve your problem, but you have a couple of bugs that are causing your program to throw away the evidence of the the real problem.

    1) This simply squashed the exception, throwing away all evidence that it ever happened.

        catch(IOException e){}
    

    2) This is a bit better, but it only prints out the exception message … not the stack trace.

        catch (Exception e){
            System.err.println(e.getMessage());
        }
    

    The other problem with 2) is that it catches ALL exceptions … not just IOExceptions. That includes unchecked exceptions like NullPointerException, SecurityException and so on.

    Exceptions should be diagnosed like this:

        catch (IOException e){
            e.printStackTrace();
        }
    

    or logged.


    Finally, the way that you are handling requests is wrong:

        Socket socket = serverSocket.accept();
        ServerThread thread = new ServerThread(i,socket);
        System.out.println(i + " Connected");
        thread.run();
    

    The mistake is in the last statement. What this actually does is to simply call the thread object’s run() method … in the current thread. To run the run() method in a different thread you need to call the start method. Like this:

        Socket socket = serverSocket.accept();
        ServerThread thread = new ServerThread(i,socket);
        System.out.println(i + " Connected");
        thread.start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a socket server in C# and a client in PHP. The
I'm creating a server that a TCP connection. The TCP Connection is run in
i am creating client sever application in windows using socket and i want to
I'm creating a browser-based game (MMO) with a server and client, all using PHP,
Have setup an ssl connection app, have already setup connection between client and server
I am creating date using following code try { newdatetime = new DateTime(2012, 2,
I try to run one program in myeclipse where after creating one web project
I'm creating a login for a website using PHP but when I try to
When creating a Socket in Java: new Socket(host, port); The Socket constructor will try
I am using shellLibrary and creating a custom desktop task bar. When I try

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.