W
wEEdpEckEr
Hi,
I try to make a chatprogram with a server and a clientside. Each one has
to run and on each one you can chat. The problem is with my sockets. I
first used one socket on both sides, but then I could only listen to it
and not write to it. So I thougt I should use 2, one to write and one to
listen. The listening on the server works, as tested with telnet, but
that's all. I know that it 's not really beautifully programmed, but the
only thing that matters now is the sockets, anyone an idea about these?
This is the servercode:
************************
class chatServer extends Thread
{
private ServerSocket serverSocket1, serverSocket2;
private Socket clientSocket1, clientSocket2;
// 1 is to write, 2 is to listen
private BufferedReader in;
private PrintWriter out;
public chatServer()
{
try {
serverSocket1 = new ServerSocket(1234);
txtOutput.setText("listening to port: 1234\n");
serverSocket2 = new ServerSocket(1235);
txtOutput.append("listening to port: 1235\n");
}
catch(IOException e) {
txtOutput.setText("Fout bij verbinden op poortnummers: 1234
of 1235\n");
txtOutput.append(e.getMessage());
}
}
public void run()
{
try {
clientSocket1 = serverSocket1.accept();
String client = clientSocket1.getInetAddress().toString();
txtOutput.append("verbinding1 gemaakt op server door " +
client + "\n");
out = new PrintWriter (clientSocket1.getOutputStream());
clientSocket2 = serverSocket2.accept();
client = clientSocket2.getInetAddress().toString();
txtOutput.append("verbinding2 gemaakt op server door " +
client + "\n");
in = new BufferedReader(new InputStreamReader
(clientSocket2.getInputStream()));
out.println("Connection established");
} catch(IOException e) {
txtOutput.append(e.getMessage());
}
String dataIn, dataOut;
try {
while ((dataIn = in.readLine()) != null) {
txtOutput.append(dataIn);
}
}
catch(IOException e) {
txtOutput.append(e.getMessage());
}
try {
out.close();
in.close();
clientSocket1.close();
clientSocket2.close();
serverSocket1.close();
serverSocket2.close();
} catch(IOException e) {
txtOutput.append("Problemen bij afsluiten van connectie:
\n");
txtOutput.append(e.getMessage());
}
}
public void sendMessage(String msg)
{
out.println(msg);
txtOutput.append(msg);
}
}
I try to make a chatprogram with a server and a clientside. Each one has
to run and on each one you can chat. The problem is with my sockets. I
first used one socket on both sides, but then I could only listen to it
and not write to it. So I thougt I should use 2, one to write and one to
listen. The listening on the server works, as tested with telnet, but
that's all. I know that it 's not really beautifully programmed, but the
only thing that matters now is the sockets, anyone an idea about these?
This is the servercode:
************************
class chatServer extends Thread
{
private ServerSocket serverSocket1, serverSocket2;
private Socket clientSocket1, clientSocket2;
// 1 is to write, 2 is to listen
private BufferedReader in;
private PrintWriter out;
public chatServer()
{
try {
serverSocket1 = new ServerSocket(1234);
txtOutput.setText("listening to port: 1234\n");
serverSocket2 = new ServerSocket(1235);
txtOutput.append("listening to port: 1235\n");
}
catch(IOException e) {
txtOutput.setText("Fout bij verbinden op poortnummers: 1234
of 1235\n");
txtOutput.append(e.getMessage());
}
}
public void run()
{
try {
clientSocket1 = serverSocket1.accept();
String client = clientSocket1.getInetAddress().toString();
txtOutput.append("verbinding1 gemaakt op server door " +
client + "\n");
out = new PrintWriter (clientSocket1.getOutputStream());
clientSocket2 = serverSocket2.accept();
client = clientSocket2.getInetAddress().toString();
txtOutput.append("verbinding2 gemaakt op server door " +
client + "\n");
in = new BufferedReader(new InputStreamReader
(clientSocket2.getInputStream()));
out.println("Connection established");
} catch(IOException e) {
txtOutput.append(e.getMessage());
}
String dataIn, dataOut;
try {
while ((dataIn = in.readLine()) != null) {
txtOutput.append(dataIn);
}
}
catch(IOException e) {
txtOutput.append(e.getMessage());
}
try {
out.close();
in.close();
clientSocket1.close();
clientSocket2.close();
serverSocket1.close();
serverSocket2.close();
} catch(IOException e) {
txtOutput.append("Problemen bij afsluiten van connectie:
\n");
txtOutput.append(e.getMessage());
}
}
public void sendMessage(String msg)
{
out.println(msg);
txtOutput.append(msg);
}
}