Reputation: 2757
I am trying to implement a chatserver with using GUI components. I implemented 3 parts(Server,Client and GUI components).
Following are a few problems I have -
Code:
First GUI component:
public class ChatServer extends javax.swing.JFrame {
String str;
public ChatServer() {
initComponents();
screen.setEditable(false);
}
private void sendActionPerformed(java.awt.event.ActionEvent evt) {
str = enter.getText();
enter.setText("");
screen.append(str+"\n");
}
public static void main(String args[]) {
new ChatServer().setVisible(true);
}
private javax.swing.JTextPane enter;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea screen;
private javax.swing.JButton send;
}
and it looks like:
This my server code:
public class Server {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(23);
System.out.println(InetAddress.getLocalHost()+" hazir");
while(true){
Socket s = ss.accept();
System.out.println(s.getInetAddress().getHostName() + " baglandi");
new ServerPart(s).start();
}
}
}
public class ServerPart extends Thread {
private Socket s;
public ServerPart(Socket s){
this.s=s;
}
@Override
public void run() {
try {
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println("Hello" + s.getInetAddress().getHostName());
String gelen;
while(true){
Scanner sc = new Scanner(s.getInputStream());
gelen = sc.nextLine();
if(gelen.trim().equalsIgnoreCase("bye"))
break;
System.out.println("Client: " + gelen);
BufferedReader input = new BufferedReader(
new InputStreamReader(System.in) );
ps.println("Server: " + input.readLine());
}
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Client code:
public class Client extends Thread {
private Socket s;
public Client(Socket s){
this.s=s;
}
@Override
public void run() {
try {
PrintStream ps = new PrintStream(s.getOutputStream());
Scanner sc = new Scanner(s.getInputStream());
ps.println("Hello" + s.getInetAddress().getHostName());
String gelen;
while(true){
BufferedReader input = new BufferedReader(
new InputStreamReader(System.in) );
gelen = sc.nextLine();
if(gelen.trim().equalsIgnoreCase("bye"))
break;
System.out.println("Client: " + gelen);
ps.println("Server: " + input.readLine());
}
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
Socket s = new Socket("192.168.1.173", 23);
new Client(s);
}
}
Would appreciate if you can help me mates.
Upvotes: 3
Views: 8939
Reputation: 26302
You are using PrintStream
class , with method ps.readline();
. If you are developing a chat application, then this approach will not work, as readline method terminate the stream when found newline or End of file, or when carriage return i.e. enter. So I do prefer to use.. datainputstream and dataoutputstream --
Socket sc = new Socket("address",port);
DataOutputStream daos;
DataInputStream dis;
dis = new DataInputStream(sc.getInputStream());
daos= new DataOutputStream(sc.getOutputStream());
Upvotes: 1