分享一个dos版的聊天室(目前只支持英文),高手帮忙解决乱码问题

原来缘来 2012-04-17 08:53:30
//刚开始学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);

}
}

...全文
148 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
原来缘来 2012-04-18
  • 打赏
  • 举报
回复
我是高中毕业然后培训出来的,以前在世纪东方国旅干的
原来缘来 2012-04-18
  • 打赏
  • 举报
回复
每届,大专还没毕业呢,跳槽了
24K純帥 2012-04-17
  • 打赏
  • 举报
回复
LZ应届的么~
原来缘来 2012-04-17
  • 打赏
  • 举报
回复
还没找到工作呢,大家所在的公司招人不?

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧