Results 1 to 3 of 3
  1. #1
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default Problems with client and server

    I am writing a SalesmanClient and a SalesmanServer, everything is working great so far, but I am a little stuck now. I have a GUI that pops up when I run the SalesmanClient that lets a user enter sales data, change sales data, read a text file, write a text file, write objects to a file and read those objects and prints them to the output text area. Now that all of that is working, I want to change the writing objects part.

    I have a writeObjectFile() method that writes the objects to a file, but what I would rather do is send that vector to the server and let the server write the objects to a file and then print that information to the Command Prompt as well. Is this something anyone could give me a few hints on? If this doesn’t make sense, just let me know and I will either post some more code for you or try to explain a little bit better.
    Here is the SalesmanServer:
    Java Code:
    import java.io.*;
     import java.net.*;
     
     public class SalesmanServer
     {
     	public static void main (String args[])
     	{
     		ServerSocket serverSocket;
     
     		try {
     			serverSocket = new ServerSocket(8000);
     
     			int clientNo = 1;
     
     			while (true)
     			{
     			Socket connectToClient = serverSocket.accept();
     
     			System.out.println("Start thread for client " + clientNo);
     
     			HandleClient thread = new HandleClient(clientNo, connectToClient);
     
     			clientNo++;
     			
     			thread.start();
     			}
     		}
     
     		catch(IOException ex)
     		{}
     	}
     }
     
     class HandleClient extends Thread
     {
     	int clientNumber;
     	Socket client;
     	public HandleClient(int num, Socket connectToClient)
     	{
     		clientNumber = num;
     		client = connectToClient;
     	}
     
     	public void run()
     	{
     		try
     		{
     			BufferedReader fromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
     
     			PrintWriter toClient = new PrintWriter(client.getOutputStream(),true);
     
     			while (true)
     			{
     
     			}
     		}
     		catch(IOException ex){}
     	}
     }
    WriteObjectFile():
    Java Code:
    public void writeObjectFile()
     	{
     		try
     		{
     			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("SalesmanThread" + String.valueOf(SalesmanClient.i) + ".data"));
     		     
     			try
     			{
     				for (Salesman s : salesmanList)
     				{
     					oos.writeObject(s);
     				}
     			}
     			finally
     			{
     				oos.close();
     			}
     		}
     		catch(IOException ioe)
     		{
     	            ioe.printStackTrace();
     	    }
     		JOptionPane.showMessageDialog(this, " Your file has been written as objects, to view it go to Read File - Object File!!", "Text File Read", JOptionPane.INFORMATION_MESSAGE);
     	}
    SalesmanClient:
    Java Code:
    public static void main (String args[])
     	{	
     		try
     		{
     			SalesmanClient app = new SalesmanClient();
     			app.setSize(700,450);
     			app.setVisible(true);
     			app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
     			
     			Socket server = new Socket("localhost", 8000);
     			System.out.println("Connected to Server:" + server.getInetAddress());
     
     			BufferedReader fromServer = new BufferedReader(new InputStreamReader(server.getInputStream()));
     
     			PrintWriter toServer = new PrintWriter(server.getOutputStream(),true);
     
     			BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
     
     			while (true)
     			{
     				
     			}
     		}
     		catch (IOException ex)
     		{	
     			System.err.println(ex);
     		}
     	}
    Thanks.

    Albert:rolleyes:

  2. #2
    Felissa is offline Member
    Join Date
    Jun 2007
    Posts
    95
    Rep Power
    0

    Default

    While you could serialize the Vector across the wire to the server I'd personally iterate through the Vector and send each item across individually. That way the server can deal with one thing at a time and they could even be mixed together - you could send an ObjectA and then an ObjectB and then another ObjectA. I really think the design will be easier.

    Greetings

    Felissa:p

  3. #3
    Albert is offline Senior Member
    Join Date
    Jun 2007
    Posts
    114
    Rep Power
    0

    Default

    Yeah that sounds like what I want to do. I think you are right, it is much better design to send one thing at a time. Thanks for your reply. This is my first time using a server and a client and I am a bit confused on the PrintWriter and BufferedReader, I get a little lost trying to figure what one is reading and what one is writing.

    Thanks!

    Albert:rolleyes:

Similar Threads

  1. how client know what kind of server
    By lemur in forum Networking
    Replies: 3
    Last Post: 05-31-2008, 08:11 AM
  2. how to send .jar files client to server
    By gobinathm in forum Networking
    Replies: 1
    Last Post: 12-25-2007, 05:05 AM
  3. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 10:18 AM
  4. Problems in running client class
    By ai_2007 in forum Advanced Java
    Replies: 0
    Last Post: 06-30-2007, 03:57 PM
  5. Simple example Client Server Application
    By ferosh in forum Networking
    Replies: 1
    Last Post: 04-01-2007, 11:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •