java socket

来源:互联网 发布:淘宝店铺短连接怎么弄 编辑:程序博客网 时间:2024/06/07 15:46

服务端:

import java.io.*;
import java.net.*;
import java.util.*;


public class ChatRoomServerExec {
private static ArrayList<Socket> alsocket = new ArrayList<Socket>();
private static Socket socketMain = null;
//主线程死循环接收用户的连接请求,接到请求后开启一个新线程,将接收到的socket传递给新线程,然后继续等待请求
//每个分支线程阻塞接收一个用户的数据,接收到数据后将其转发给其它的所有用户
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(888);
while (true) {
socketMain = ss.accept();
System.out.println("接收到新的连接,ip: " + socketMain.getInetAddress().getHostAddress() + " port: "
+ socketMain.getPort());
alsocket.add(socketMain); // 每收到一个连接都会添加到ArrayList中
//本线程将新用户上线的消息广播,然后阻塞接收这个新用户的消息,接收到后广播给所有其它用户
new Thread() {
public void run() {   
Socket socketThread = socketMain;
try {
String strTemp = String.format("【系统提示】: %s上线了",
socketThread.getInetAddress().getHostAddress() + "&&" + socketThread.getPort());
System.out.flush();
for (Socket Temp : ChatRoomServerExec.alsocket) {
OutputStream os = Temp.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println(strTemp);
}
InputStream is = socketThread.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String strRead = "";
// byte [] arb=new byte[1024];     //接收数据方案2  接收字节流   1-2不能交叉收发
// int iReadCount=0;    //接收数据方案2 接收字节流
while (true) {
strRead = br.readLine(); // 接收数据方案1 接收字符串
// iReadCount=is.read(arb);//接收数据方案2 接收字节流
// strRead=new String(arb,0,iReadCount);//接收数据方案2 接收字节流
strRead="【"+socketThread.getInetAddress().getHostAddress() + "&&" + socketThread.getPort()+"】说: "+strRead;
for (Socket Temp : ChatRoomServerExec.alsocket) {
if (!Temp.getInetAddress().getHostAddress().equals(socketThread.getInetAddress().getHostAddress()) || Temp.getPort() != socketThread.getPort()) {
OutputStream os = Temp.getOutputStream();
PrintStream ps = new PrintStream(os);
ps.println(strRead);
os.flush();
}
}
}
} catch (Exception e) {
// System.out.println("子线程抛出异常,异常类型是: " + e.getClass().getName());
// System.out.println("异常信息是: " + e.getMessage());
alsocket.remove(alsocket);
System.out.println("【系统消息】: "+socketThread.getInetAddress().getHostAddress() + "&&" + socketThread.getPort()+"下线了 ");
}
}
}.start();
}


} catch (Exception e) {
System.out.println("主线程抛出异常,异常类型是: " + e.getClass().getName());
System.out.println("异常信息是: " + e.getMessage());
}
}
}


客户端:

import java.io.*;
import java.net.*;


public class ChatRoomUserExec {
private static Socket socketMain=null;
//主线程阻塞接受用户的输入,然后立刻发送给服务器
//开一个分支线程接受服务器发送的数据,然后显示在屏幕上
public static void main(String[] args) {
try{
socketMain=new Socket(InetAddress.getByAddress(new byte[]{127,0,0,1}),888);
new Thread(){
public void run(){
try{
Socket socketThread=socketMain;
InputStream is=socketThread.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String strRead="";
while(true){
strRead=br.readLine();//阻塞读入数据,本行代码可能有问题
System.out.println(strRead);
System.out.flush();
}
}
catch(Exception e){
System.out.println("子线程抛出异常,异常类型是: "+e.getClass().getName());
System.out.println("异常信息是: "+e.getMessage());
}
}
}.start();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String strTemp="";
OutputStream os=socketMain.getOutputStream();
PrintStream ps = new PrintStream(os);     //本行代码是可以工作的
while(true){
strTemp=br.readLine();      
//以下是测试代码
// os.write(strTemp.getBytes());   //方案2 发送字节流   方案1和方案2不能交叉收发,否则会收不到
//测试代码结束
ps.println(strTemp);           //方案1 发送字符串 
os.flush();
System.out.println("【我】说: "+strTemp);
}
}
catch(Exception e){
System.out.println("主线程抛出异常,异常类型是: "+e.getClass().getName());
System.out.println("异常信息是: "+e.getMessage());
}
}
}

0 0
原创粉丝点击