服务器的:
packageliaotian;
importjava.io.BufferedReader;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjava.net.ServerSocket;
importjava.net.Socket;
importjava.util.Date;
publicclassserver{
publicserver(){
try{
//设置sever端的链接
ServerSockets=newServerSocket(6667);//创建一个ServerSocket套接字对象,并绑定在6667端口上
Socketss=s.accept();//声明一个Socket对象
//实例化输出流
OutputStreamout=ss.getOutputStream();
DataOutputStreamdout=newDataOutputStream(out);
//实例化输入流
InputStreamin=ss.getInputStream();
DataInputStreamdin=newDataInputStream(in);
System.out.print(din.readUTF()+"!");
dout.writeUTF("你已经连接到服务器"+"\n"+"你的地址:"+ss.getInetAddress()
+"\n"+"你的链接端口:"+ss.getLocalPort()+"\n");
//实例化两个线程的对象并启动线程
newReadMessage(din).start();
newSendMessage(dout).start();
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
newserver();
}
}
//接受客户端信息
classReadMessageextendsThread{
privateDataInputStreamdin;
publicReadMessage(DataInputStreamdin){
this.din=din;
}
publicvoidrun(){
Stringstr;
try{
while(true){
str=din.readUTF();
System.out.println(newDate().toLocaleString()+"客户端说:"+str);
if(str.equals("bye")){
System.out.println("客户端下线!");
break;
}
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
//发出服务器信息
classSendMessageextendsThread{
privateDataOutputStreamdout;
publicSendMessage(DataOutputStreamdout){
this.dout=dout;
}
publicvoidrun(){
InputStreamReaderinr=newInputStreamReader(System.in);
BufferedReaderbuf=newBufferedReader(inr);
Stringstr;
try{
while(true){
str=buf.readLine();
dout.writeUTF(str);
if(str.equals("bye")){
System.out.println("服务器退出!");
System.exit(1);
}
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
客户端:
packageliaotian;
importjava.io.BufferedReader;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.OutputStream;
importjava.net.Socket;
importjava.util.Date;
publicclassclient{
publicclient(){
try{
//设置sever端的链接
Sockets=newSocket("127.0.0.1",6667);//监听端口号6667
//实例化输入流
InputStreamin=s.getInputStream();
DataInputStreamdin=newDataInputStream(in);
//实例化输出流
OutputStreamout=s.getOutputStream();
DataOutputStreamdout=newDataOutputStream(out);
dout.writeUTF("服务器你好!我是客户端");
System.out.println(din.readUTF());
//实例化两个线程的对象并启动线程
newThread(newSenderMessage(dout)).start();
newThread(newReaderMessage(din)).start();
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
newclient();
}
}
classReaderMessageimplementsRunnable{
privateDataInputStreamdin;
publicReaderMessage(DataInputStreamdin){
this.din=din;
}
publicvoidrun(){
Stringstr;
try{
while(true){
str=din.readUTF();
System.out.println(newDate().toLocaleString()+"服务器说:"+str);
if(str.equals("bye")){
System.out.println("服务器已经关闭,此程序自动退出!");
break;
}
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
classSenderMessageimplementsRunnable{
privateDataOutputStreamdout;
publicSenderMessage(DataOutputStreamdout){
this.dout=dout;
}
publicvoidrun(){
Stringstr;
InputStreamReaderinf=newInputStreamReader(System.in);
BufferedReaderbuf=newBufferedReader(inf);
try{
while(true){
str=buf.readLine();
dout.writeUTF(str);
if(str.equals("bye")){
System.out.println("客户端自己退出!");
System.exit(1);
}
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}