Reputation: 35
I recently started learning out Sockets and other Networking Stuff like that works, I made a simple Chat program with a GUI, it works somewhat, the problem is that If order the message to send to the server the client must put the same message twice, and if the server sends a message, the client must reply once.
Heres my Client/Server Class:
package org.codingllamas.Chat;
import java.io.*;
import java.net.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTML;
public class SC {
static Socket clientSocket;
static BufferedReader inFromServer;
static DataOutputStream outToServer;
public static void clientSetup(int port,String ip) throws IOException, BadLocationException {
String modifiedSentence;
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Connected to </b>" + ip + ":" + port + "<br>",0,0,HTML.Tag.B);
while (true) {
///BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
clientSocket = new Socket(ip,port);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
modifiedSentence = inFromServer.readLine();
//System.out.println(modifiedSentence);
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Server: </b>" + modifiedSentence + "<br>",0,0,HTML.Tag.B);
//clientSocket.close();
}
}
public static void clientSend(String msg) throws IOException, BadLocationException{
outToServer.writeBytes(msg + "\r");
outToServer.flush();
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>You: </b>" + msg + "<br>",0,0,HTML.Tag.B);
}
static ServerSocket welcomeSocket;
static Socket connectionSocket;
static BufferedReader inFromClient;
static DataOutputStream outToClient;
public static void serverSetup(int port) throws Exception {
String clientSentence;
welcomeSocket = new ServerSocket(port);
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Server Started on port: " + port + "</b><br>",0,0,HTML.Tag.B);
while(true){
connectionSocket = welcomeSocket.accept();
inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
clientSentence = inFromClient.readLine();
outToClient = new DataOutputStream(connectionSocket.getOutputStream());
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>Parther: </b>" + inFromClient.readLine() + "<br>",0,0,HTML.Tag.B);
}
}
public static void serverSend(String msg) throws IOException, BadLocationException {
//DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
outToClient.writeBytes(msg + "\r");
outToClient.flush();
Start.kit.insertHTML(Start.doc,Start.doc.getLength(),"<b>You: </b>" + msg + "<br>",0,0,HTML.Tag.B);
}
}
And a picture:
Thanks in advance.
Upvotes: 1
Views: 244
Reputation: 310980
DataOutputStream
, use a BufferedWriter.
modifiedSentence?
: use a single Socket
for the life of the client, and loop reading lines, and close the socket when you get null.accept():
start a new thread to read lines like the client does now.Upvotes: 1