sockets on client/server in threads?

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);
}
}
 
X

_XaToA_

Can you send me all the code and i'll try to help you?
if you accept send to (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top