I’m experiencing some problems with sockets.
I’m trying to make a concurrent server to accept more than one clients.
When a client connects to the server, the server creates a new thread and listens to the socket.
Then, if the client sends something, the server must read it.
On the client i just open a dictionary (txt form) and i send it over the socket.
On the server the only thing i get is this:
#
null
SERVER
try
{
String message,file = new String("StressTextFile.txt");
File filee = new File(file);
long length;
// Open the file
FileInputStream fstream = new FileInputStream(file);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine = new String();
length = filee.length();
ProgressBar.setMaximum((int)length); //we're going to get this many bytes
ProgressBar.setValue(0); //we've gotten 0 bytes so far
//Read File Line By Line
font1 = new Font("Helvetica", Font.PLAIN, 18);
font1.isBold();
color = new Color( 74,118,110);
TextArea1.setForeground(color);
TextArea1.setFont(font1);
int soma=0;
while(in.readLine() != null)
{
strLine = in.readLine(); // reads from file
//System.out.println(strLine);
TextArea1.append(strLine+"\n");
pwOut.write(strLine);
pwOut.flush();
soma+=strLine.length()+1;
ProgressBar.setValue(ProgressBar.getValue()+(strLine.length()+1)*2);
ProgressBar.repaint();
};
br.close();
pwOut.close();
Skt.close();
}catch (Exception e){ System.err.println("Error: " + e.getMessage()); }
}
CLIENT
try
{
brIn = new BufferedReader(new InputStreamReader(skt.getInputStream()));
while(s != null)
{
s = brIn.readLine();
System.out.println("#####");
System.out.println(s);
}
}
catch (IOException e1) {e1.printStackTrace(); }
Please forget the Swing components.
I think all socket stuff are ok. why can’t i get nothing on the server side?
Please help
Kind regards
A barebones example of a multi-threaded server:
And your server thread:
Missing from this example:
close()ing the sockets/streamsHopefully that gives you some idea of how it usually looks.