Hi all,
I could really use some help with a multithreaded server/client application that I'm building. I'm first experimenting with basic concepts and then am going to implement the results within a larger application. However, I find myself stuck on a particular issue. Initially, I transfer a file from the client to the server. This works fine. However, once this transfer has been completed I wish to send additional messages between server and client. This is where the problem lies as the client and server both hang on their respective sides. I really don't know how to resolve this problem. I did implement closing the socket and reopening another and this worked fine when running a single client. However, when running multiple clients I encountered problems that are self-explanatory. So I need another solution that doesn't involve closing and reopening the socket.
Here is my server code:
PHP Code:
import java.io.*;
import java.net.*;
public class Provider implements Runnable{
ServerSocket server;
BufferedInputStream bis;
BufferedOutputStream bos;
InputStreamReader isr;
Socket sock;
int in;
PrintWriter outputStream;
int count = 0;
public void run()
{
outputStream = null;
try{
server = new ServerSocket(1517);
while (true)
{
sock = server.accept();
ClientThread c = new ClientThread(sock,count);
Thread t = new Thread(c);
t.start();
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
finally {
try {
sock.close();
server.close();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
public static void main(String args[])
{
Provider Server = new Provider();
Server.run();
}
}
Here is my ClientThread code:
PHP Code:
import java.io.*;
import java.net.*;
public class ClientThread implements Runnable {
ServerSocket server;
BufferedInputStream bis;
BufferedOutputStream bos;
InputStreamReader isr;
Socket sock;
int in;
PrintWriter outputStream;
int ID = 0;
ClientThread(Socket s, int ID){
this.sock = s;
this.ID = ID;
}
public void run() {
try {
bis = new BufferedInputStream(sock.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream("C:/Amanda_test/test"));
byte[] byteArray = new byte[6022386];
while ((in = bis.read(byteArray)) != -1)
{
bos.write(byteArray,0,in);
}
bos.flush();
BufferedReader input = new BufferedReader(
new InputStreamReader(
sock.getInputStream()));
PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
// not sure whether this is necessary
// just thought that it would be bad form for the client to wait
// for the server to write a message first. I've tried with and
// without the readLine(), though, and it makes no difference
input.readLine();
out.println("success"); // necessary
out.flush();
out.close();
input.close();
bos.close();
bis.close();
this.sock.close();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
and this is my Client code:
PHP Code:
import java.io.*;
import java.net.*;
public class Requester{
Socket requestSocket;
BufferedOutputStream bos;
BufferedInputStream bis;
Socket sock;
int in;
Requester(){}
void run()
{
try{
//1. creating a socket to connect to the server
sock = new Socket("myServer.ac.za", 1517);
System.out.println("Connected to localhost in port 1517");
File myFile = new File("F:/Documents.txt");
bis = new BufferedInputStream(new FileInputStream(myFile));
bos = new BufferedOutputStream(sock.getOutputStream());
byte[] byteArray = new byte[(int)myFile.length()];
System.out.println((int)myFile.length());
while((in = bis.read(byteArray))!= -1)
{
bos.write(byteArray,0,in);
}
bos.flush();
BufferedReader input = new BufferedReader(
new InputStreamReader(
sock.getInputStream()));
PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
// not sure whether this is necessary
// just thought that it would be bad form for the client to wait
// for the server to write a message first. I've tried with and
// without the println(), though, and it makes no difference
out.println("maybe no reason for this");
String word = input.readLine(); // necessary
out.flush();
out.close();
input.close();
bos.close();
bis.close();
sock.close();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
Help would be greatly appreciated!
Thanks.