//刚开始学socket的时候,搜一个聊天程序没有一个能正常运行的,现在来分享一下自己写的package com.me.learn.socket;
public interface IChar {
int port = 5198;
}package com.me.learn.socket;
/**
 * 
 * @author JayYounger
 * created 120417
 *
 */
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;public class ChatRoomServer implements IChar{
private Set<Socket> sockets= new HashSet<Socket>();
private BufferedReader in;

public static void main(String[] args) {
try {
new ChatRoomServer().service(port);
} catch (Exception e) {
e.printStackTrace();
}
}

public void service(int port) throws Exception{
ServerSocket server = new ServerSocket(port);
while(true){
final Socket client = server.accept();
System.out.println(client.getInetAddress());
sockets.add(client);
new Thread(new Runnable() {
@Override
public void run() {
try {
getMsg(client);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}

public void sendMsg(Socket currentClient,String msg) throws IOException{
PrintWriter out= null;
for(Socket client : sockets){
if(client.isConnected() && client!=currentClient){
out = new PrintWriter(new BufferedOutputStream(client.getOutputStream()));
out.println(client.getInetAddress()+"say: \n"+msg);
out.flush();
}
}
}

public void getMsg(final Socket client) throws IOException{
final BufferedReader in2  = new BufferedReader(new InputStreamReader(client.getInputStream()));
new Timer().scheduleAtFixedRate(new TimerTask() {
public void run() {
try {
if (in2.ready()) {
String msg = in2.readLine();
//logMsg(msg);
sendMsg(client,msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}, 0, 1000);
}

public void logMsg(String msg){
try {
System.out.println(new String(msg.getBytes("UTF-8"),"GB2312"));
System.out.println(new String(msg.getBytes("GBK"),"GB2312"));
System.out.println(new String(msg.getBytes("ISO8859-1"),"GB2312"));
System.out.println(new String(msg.getBytes("UTF-8"),"ISO8859-1"));
System.out.println(new String(msg.getBytes("GB2312"),"ISO8859-1"));
System.out.println(new String(msg.getBytes("GBK"),"ISO8859-1"));
System.out.println(new String(msg.getBytes("UTF-8"),"GBK"));
System.out.println(new String(msg.getBytes("GB2312"),"GBK"));
System.out.println(new String(msg.getBytes("ISO8859-1"),"GBK"));
System.out.println(new String(msg.getBytes("ISO8859-1"),"UTF-8"));
System.out.println(new String(msg.getBytes("GB2312"),"UTF-8"));
System.out.println(new String(msg.getBytes("GBK"),"UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void closeSocket(Socket s){
if(s!=null && s.isConnected()){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package com.me.learn.socket;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;/**
 * 
 * @author JayYounger created 120417
 * 
 */
public class ChatRoomClient implements IChar {
private PrintWriter out;
private BufferedReader in;
private Socket client = null; public static void main(String[] args) {
try {
new ChatRoomClient().connect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void connect() throws Exception {
client = new Socket("localhost", port);
out = new PrintWriter(client.getOutputStream());
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
new Thread(new Runnable() {
@Override
public void run() {
sendMsg();
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
getMsg();
}
}).start();
}
/**
 * 发送信息
 */
public void sendMsg() {
Scanner scanner = new Scanner(System.in);
try {
while (!client.isClosed()) {
System.out.println("请输入内容");
String line = scanner.nextLine();
if ("byebye".equals(line)) {
in.close();
client.close();
break; }
out.println(line);
out.flush();
}
} catch (Exception e) {
System.out.println(e);
}
}
/**
 * 得到其他client的Msg
 */
public void getMsg() {
new Timer().scheduleAtFixedRate(new TimerTask() {
public void run() {
try {
if (in.ready()) {
String line = in.readLine();
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}, 0, 1000); }
}