聊天室基本代码.doc
客端:户户户
package TCP传传;
import java.io.DataOutputStream;import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;public class Client {
public static void main(String[] args) {
Socket s = null;
DataOutputStream dos = null;
try {
s = new Socket("localhost",1234);
dos = new DataOutputStream(s.getOutputStream());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dos.writeUTF("有人在?");传
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
if(dos!=null){
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服端户户
package TCP传传;
import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
Socket s;
ServerSocket ss = null;
try {
ss = new ServerSocket(1234);
while(true){
s = ss.accept();
//System.out.print(s.getRemoteSocketAddress()+"传:" );
Runnable r = new Inputdata(s);
Thread t = new Thread(r);
t.start();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
class Inputdata implements Runnable{
Socket sss;
public Inputdata() {
super();
}
public Inputdata(Socket sss) {
super();
this.sss = sss;
}
public void run(){
DataInputStream dis = null;
try {
dis = new DataInputStream(sss.getInputStream());
// dis.readUTF();
System.out.print(sss.getRemoteSocketAddress()+"传:" );
System.out.println(dis.readUTF());
} catch (IOException e) {
e.printStackTrace();
}finally{
if(dis!=null){
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
本文档为【聊天室基本代码.doc】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。