奇怪的聊天程序
本人是java的菜鸟中的菜鸟,现有一个问题实在是不懂,以下是本人写的一段聊天程序的代码:
服务器端的:
import java.net.*;
import java.io.*;
public class client1 implements Runnable {
Socket client;
ServerSocket server;
BufferedReader dis;
DataOutputStream dos;
public void input(){
try {
Boolean flag=true;
while(flag){
InputStream is=client.getInputStream();
dis=new BufferedReader(new InputStreamReader(is));
String ch;
while((ch=dis.readLine())!=null){
System.out.println(ch);
}
}
}
catch(Exception e){
}
}
public void run(){
try{
OutputStream os=client.getOutputStream();
dos=new DataOutputStream(os);
int ch;
while((ch=System.in.read())!=-1){ if(ch== '\n '){
dos.flush();
}
dos.write(ch);
}
}
catch(Exception e){
}
}
public void output(){
try{
server=new ServerSocket(1234);
client =server.accept();
}
catch(Exception e){}
Thread a=new Thread(this);
a.start();
input();
}
public static void main(String[] args) {
client1 b=new client1();
b.output();
}
}
客户端的:
import java.net.*;
import java.io.*;
public class client1 implements Runnable {
Socket client;
BufferedReader dis;
DataOutputStream dos;
public void input(){
try {
Boolean flag=true;
while(flag){
InputStream is=client.getInputStream();
dis=new BufferedReader(new InputStreamReader(is));