Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-02-2007, 06:59 AM
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
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:
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():
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:
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-02-2007, 07:03 AM
Member
 
Join Date: Jun 2007
Posts: 95
Felissa is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-02-2007, 07:07 AM
Senior Member
 
Join Date: Jun 2007
Posts: 114
Albert is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT +3. The time now is 08:32 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org