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 8322171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:17:26+00:00 2026-06-08T23:17:26+00:00

i try to program a part of a university research project about multi client

  • 0

i try to program a part of a university research project about multi client – server socket programming.my code works as well as so that i give valide result but the problem is that evalutor of our group said that my code have not a good speed on connection for data transfer.i will be thankfull if you found the problem(s) in my code that cause this issue.

server part:

    import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import j

ava.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

/**
 * 
 */

/**
 * @author Sina
 *
 */
public class BoxServer {

    ServerSocket serversocket;
    static ThreadHandler t[]=new ThreadHandler[100];
    static int size=0;
    static ArrayList<Message> messagebox=new ArrayList<Message>();
    public static void main(String[] args) {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(79);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        while(true)
        {

        try{




                //InetAddress inetadress=InetAddress.getLocalHost();
                //System.out.println(inetadress);
                //System.out.println(inetadress.getHostName());
                //System.out.println(inetadress.getHostAddress());
                Socket socket=serverSocket.accept();
                if(socket==null)
                {
                    System.out.println("null");
                }
                t[size]=new ThreadHandler(socket,"username");
                size++;
                t[size-1].start();



        }
        catch(UnknownHostException e){
            System.out.println("salam s");
         System.out.println(e.getMessage());
        }
        catch (IOException e) {
            System.out.println("bye s");
            System.out.println(e.getMessage());
        }


        }

    }

}

class ThreadHandler extends Thread{

    private String socname;
    Socket mySocket;
    ObjectInputStream inp;
    ObjectOutputStream outp;
    public ThreadHandler(Socket s,String socketName)
    {
        this.mySocket=s;
        this.socname=socketName;



    }
    public void run()
    {
        try {
            inp=new ObjectInputStream(mySocket.getInputStream());
            outp=new ObjectOutputStream(mySocket.getOutputStream());
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        while(true)
        {
        System.out.println("thread run");
        System.out.println(mySocket.getLocalPort());
        System.out.println(mySocket.getLocalAddress());

        try {
        //  System.out.println("my socket:"+mySocket.getOutputStream());
            System.out.println(mySocket.isConnected());
            System.out.println(inp.available());
            System.out.println("inp = "+inp);
            System.out.println("reeead "+ inp.readObject());
            Message mess=(Message)inp.readObject();
            System.out.println("dsd");
            System.out.println("mess: "+mess);
            BoxServer.messagebox.add(mess);
            if(mess.getReceiver().equals("system-use:code=1"))
            {
                System.out.println(mess.getSender()+" wants to see his/her Inbox");
            }
            //mySocket.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("bug dar thread");
            e.printStackTrace();

        }

         }

    }
}

client part

    import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;

import javax.swing.Timer;

public class Main {

    /**
     * @param args
     */


    static Socket socket=new Socket();
    public static void main(String[] args) {




        System.out.println("newuser(n) or login(l)");
        Scanner scanner=new Scanner(System.in);
        String typeOfOperation=scanner.nextLine();
        if(typeOfOperation.equals("n"))
        {


        }
        else
        if(typeOfOperation.equals("l"))
        {

            try {
                socket = new Socket("127.0.0.1",79);
                final ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
                ObjectInputStream in=new ObjectInputStream(socket.getInputStream());

                while(true)
                {

                Thread timer=new Thread()
                {
                    public void run()
                    {
                        while(true)
                        {
                            Message temp=new Message();
                            temp.setReceiver("system-use:code=1");
                            temp.setSender("username");

                            try {
                                out.writeObject(temp);
                                sleep(5000);
                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            catch (InterruptedException e) {
                                // TODO: handle exception
                                e.printStackTrace();
                            }

                        }
                    }
                };
                timer.start();
                String username=scanner.nextLine();
                String to=scanner.nextLine();
                String body=scanner.nextLine();
                Message all=new Message();
                all.setText(body);
                all.setReceiver(to);
                all.setSender(username);


                System.out.println("you connected to system");
                System.out.println(socket);

                System.out.println("now should write");
                out.writeObject(all);
                System.out.println("ghable threAD");

                }
            //  socket.close();

            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                System.out.println("salaam c");
                System.out.println(e.getMessage());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                System.out.println("bye c");
                System.out.println(e.getMessage());
            }

        }
        else
        {
            System.out.println("bad operation. try again!");
        }


    }

}

Message class(Entity only not important i think!):

import java.io.Serializable;


public class Message implements Serializable{
    String sender;
    String receiver;
    String text;
    boolean delivered=false;
    public void delived()
    {
        this.delivered=true;
    }
    private String tostringOfClass;

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }
    public void setSender(String sender) {
        this.sender = sender;
    }
    public void setText(String text) {
        this.text = text;
    }
    public String getReceiver() {
        return receiver;
    }
    public String getSender() {
        return sender;
    }
    public String getText() {
        return text;
    }

    public String toString()
    {
        tostringOfClass="sender : "+sender+" \n"+"receiver : "+receiver+" \n"+"message: "+text;

        return tostringOfClass;
    }

}
  • 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-08T23:17:28+00:00Added an answer on June 8, 2026 at 11:17 pm

    Your evaluator missed something much more important: it doesn’t work. You are calling readObject() twice per loop in the server, but all you do with the first result is print it out with System.out.println(). So your code is missing every odd object.

    There isn’t much you could do to improve the speed of this. He probably wants you to interpose a BufferedOutputStream between the ObjectOutputStream and the socket, and similarly for BufferedInputStream. However the object streams already run their own buffers, so this is probably a waste of time. He might also want you to use large socket send and receive buffers, if you’ve been taught about those: see Socket.setXXXBufferSize(). Set them to at least 32k. He might also be anti-Serialization, but for this application I don’t see that it makes much difference. This is an interactive application, and the messages are small, so the speed over the network is basically irrelevant. You can only type so fast.

    You should also close in the client when the user types whatever it is that tells the program to stop, and in the server you must catch EOFException, before IOException, and close the socket and break out of the read loop when you get it.

    Also printing out Socket.isConnected() yields no useful information. The socket is always connected at the points you print it at. This method is not a health-check for the connection, it only tells you about the state of your Socket. Not the same thing.

    Your evaluator seems to me to be focussing on entirely the wrong thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in a part of my code i write in socket from server and wait
These code is the Server part of my proxy program, and its function is
I try to run one program in myeclipse where after creating one web project
As part of our University coursework we have to make a multi threading download
Here is a part from my program: try { Class.forName(com.mysql.jdbc.Driver); InitializeData data = new
I have a client-server program. I'm sending data like this: private void Sender(string s,TcpClient
The following is part of client program for a fraction class. I wrote the
When I try to execute an external program from java I use this code
I'm trying to program something like facebook recent update part,so I program following code
When i try to compile this as part of an objective-c program it gives

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.