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:
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():
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:
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