我想要一个用java网络编程写的点对点聊天程序(TCP和UDP)?

服务器端:

导入Java . io . *;

导入Java . net . *;

导入Java . applet . applet;

公共类TalkServer{

公共静态void main(String args[]) {

尝试{

ServerSocket server = null

尝试{

server = new server socket(4700);

}catch(异常e) {

System.out.println("不能听:"+e);

}

Socket socket = null

尝试{

socket = server . accept();

}catch(异常e) {

System.out.println("Error。"+e);

}

弦线;

buffered reader is = new buffered reader(new InputStreamReader(socket . getinputstream()));

PrintWriter OS = new PrintWriter(socket . get output stream());

buffered reader sin = new buffered reader(new InputStreamReader(system . in));

system . out . println(" Client:"+is . readline());

line = sin . readline();

而(!line.equals("拜拜"){

OS . println(line);

OS . flush();

system . out . println(" Server:"+line);

system . out . println(" Client:"+is . readline());

line = sin . readline();

}

OS . close();

is . close();

socket . close();

server . close();

}catch(异常e){

System.out.println("错误:"+e);

}

}

}

客户端:

导入Java . io . *;

导入Java . net . *;

公共类TalkClient {

公共静态void main(String args[]) {

尝试{

Socket socket =新插座(" 127.0.0.1 ",4700);

buffered reader sin = new buffered reader(new InputStreamReader(system . in));

PrintWriter OS = new PrintWriter(socket . get output stream());

buffered reader is = new buffered reader(new InputStreamReader(socket . getinputstream()));

字符串readline

readline = sin . readline();//从系统标准输入中读取字符串。

而(!readline.equals("拜拜"){

OS . println(readline);

OS . flush();

system . out . println(" Client:"+readline);

system . out . println(" Server:"+is . readline());

readline = sin . readline();//从系统标准输入中读取字符串。

}

OS . close();//关闭套接字输出流

is . close();//关闭套接字输入流

socket . close();//关闭套接字

}catch(异常e) {

system . out . println(" Error "+e);//如果有错误,将打印错误消息。

}

}

}