这是本人写的一个简易的聊天室功能简单。但是易懂,知识点丰富。。适合新手阅读。。O(∩_∩)O哈哈~我也是新手O(∩_∩)O哈哈~同时针对我这个聊天程序向高手们请教一下
一、怎么样才能在防火墙开这的情况下依然能够通信?
二、客户端连接服务器的速度很慢。。有什么办法加快吗??
三、怎样使客户端具有自动探索局域网中的服务器???有兴趣并想和一起做这样一个聊天室的朋友们可以和我联系。。我的QQ:563980247希望高手指点啊。加我QQ啊。我要拜师。我要拜师。我要拜师。//服务器端////////////////////////////
import java.net.*;
import java.io.*;
import java.util.*;public class ChatServer {
List<Client> clients=new ArrayList<Client>();
Socket s;
ServerSocket ss;
public static void main(String[] args) {
new ChatServer().start();
}
public void start(){
try {
ss=new ServerSocket(6666);
while(true){
s=ss.accept();
//System.out.println("connect complete!\n");
Client c=new Client(s);
clients.add(c);
new Thread(c).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("端口使用中...");
System.exit(-1);
}
}
class Client implements Runnable {
Socket s;
String str;
BufferedReader bfr;
PrintStream ps;
Client(Socket s){
this.s=s;
}
public void run() {
try {
ps=new PrintStream(s.getOutputStream());
BufferedReader bfr=new BufferedReader(new InputStreamReader(s.getInputStream()));
while(true){
str=bfr.readLine();
if (str==null) {
str=s.getInetAddress().getHostName()+"下线了!";
System.out.println(str);
for(int i=0;i<clients.size();i++){
Client c=clients.get(i);
c.ps.println(str);
}
bfr.close();
s.close();
break;
}
for(int i=0;i<clients.size();i++){
Client c=clients.get(i);
c.ps.println(str);
}
System.out.print(str+"\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
for(int i=0;i<clients.size();i++){
Client c=clients.get(i);
c.ps.println(s.getInetAddress().getHostName()+"下线了");
}
}
}
}}
//客户端///////////////////////////
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.*;
import java.net.*;public class ChatClient extends Frame{
TextField tf;
TextArea taSum;
Socket s;
//OutputStream os;
PrintStream ps;
BufferedReader br;
String name;
public static void main(String[] args) {
ChatClient cc=new ChatClient();
// cc.launchFrame();
cc.connect();
cc.launchFrame();
cc.receive();
}
public void launchFrame(){
setTitle("聊天室");
setBounds(new Rectangle(200,200,400,200));
setResizable(false);
setLayout(new BorderLayout());
tf=new TextField();
taSum=new TextArea("",8,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
add(tf,BorderLayout.SOUTH);
add(taSum,BorderLayout.NORTH);
taSum.setEditable(false);
taSum.setBackground(Color.PINK);
tf.addActionListener(new tfMonitor()); this.addWindowListener(new WindowAdapter(){ @Override
public void windowClosing(WindowEvent arg0) {
try {
ps.close();
//os.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setVisible(false);
System.exit(0);
}
});
pack();
setVisible(true);
}
class tfMonitor implements ActionListener { public void actionPerformed(ActionEvent e) {
ps.println(name+"说:"+tf.getText());
//taSum.append(name+tf.getText());
//taSum.append("\n");
tf.setText("");
}
}
public void connect(){
try {
s=new Socket("SkySmile",6666);
System.out.println("服务器连接成功!\n");
//os=s.getOutputStream();
ps=new PrintStream(s.getOutputStream());
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
name=s.getLocalAddress().getHostName();
ps.println(name+"上线了!");
} catch (UnknownHostException e) {
System.out.println("IP地址错误!");
System.exit(-1);
} catch (IOException e) {
System.out.println("服务器连接失败!");
System.exit(-1);
}
}
public void receive(){
String str;
while(true){
try {
str=br.readLine();
if(str==null) break;
taSum.append(str+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
taSum.append("与服务器断开连接!");
try {
br.close();
ps.close();
s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
//////////////////////////////////////////////////
一、怎么样才能在防火墙开这的情况下依然能够通信?
二、客户端连接服务器的速度很慢。。有什么办法加快吗??
三、怎样使客户端具有自动探索局域网中的服务器???有兴趣并想和一起做这样一个聊天室的朋友们可以和我联系。。我的QQ:563980247希望高手指点啊。加我QQ啊。我要拜师。我要拜师。我要拜师。//服务器端////////////////////////////
import java.net.*;
import java.io.*;
import java.util.*;public class ChatServer {
List<Client> clients=new ArrayList<Client>();
Socket s;
ServerSocket ss;
public static void main(String[] args) {
new ChatServer().start();
}
public void start(){
try {
ss=new ServerSocket(6666);
while(true){
s=ss.accept();
//System.out.println("connect complete!\n");
Client c=new Client(s);
clients.add(c);
new Thread(c).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("端口使用中...");
System.exit(-1);
}
}
class Client implements Runnable {
Socket s;
String str;
BufferedReader bfr;
PrintStream ps;
Client(Socket s){
this.s=s;
}
public void run() {
try {
ps=new PrintStream(s.getOutputStream());
BufferedReader bfr=new BufferedReader(new InputStreamReader(s.getInputStream()));
while(true){
str=bfr.readLine();
if (str==null) {
str=s.getInetAddress().getHostName()+"下线了!";
System.out.println(str);
for(int i=0;i<clients.size();i++){
Client c=clients.get(i);
c.ps.println(str);
}
bfr.close();
s.close();
break;
}
for(int i=0;i<clients.size();i++){
Client c=clients.get(i);
c.ps.println(str);
}
System.out.print(str+"\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
for(int i=0;i<clients.size();i++){
Client c=clients.get(i);
c.ps.println(s.getInetAddress().getHostName()+"下线了");
}
}
}
}}
//客户端///////////////////////////
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.io.*;
import java.net.*;public class ChatClient extends Frame{
TextField tf;
TextArea taSum;
Socket s;
//OutputStream os;
PrintStream ps;
BufferedReader br;
String name;
public static void main(String[] args) {
ChatClient cc=new ChatClient();
// cc.launchFrame();
cc.connect();
cc.launchFrame();
cc.receive();
}
public void launchFrame(){
setTitle("聊天室");
setBounds(new Rectangle(200,200,400,200));
setResizable(false);
setLayout(new BorderLayout());
tf=new TextField();
taSum=new TextArea("",8,50,TextArea.SCROLLBARS_VERTICAL_ONLY);
add(tf,BorderLayout.SOUTH);
add(taSum,BorderLayout.NORTH);
taSum.setEditable(false);
taSum.setBackground(Color.PINK);
tf.addActionListener(new tfMonitor()); this.addWindowListener(new WindowAdapter(){ @Override
public void windowClosing(WindowEvent arg0) {
try {
ps.close();
//os.close();
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setVisible(false);
System.exit(0);
}
});
pack();
setVisible(true);
}
class tfMonitor implements ActionListener { public void actionPerformed(ActionEvent e) {
ps.println(name+"说:"+tf.getText());
//taSum.append(name+tf.getText());
//taSum.append("\n");
tf.setText("");
}
}
public void connect(){
try {
s=new Socket("SkySmile",6666);
System.out.println("服务器连接成功!\n");
//os=s.getOutputStream();
ps=new PrintStream(s.getOutputStream());
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
name=s.getLocalAddress().getHostName();
ps.println(name+"上线了!");
} catch (UnknownHostException e) {
System.out.println("IP地址错误!");
System.exit(-1);
} catch (IOException e) {
System.out.println("服务器连接失败!");
System.exit(-1);
}
}
public void receive(){
String str;
while(true){
try {
str=br.readLine();
if(str==null) break;
taSum.append(str+"\n");
} catch (IOException e) {
// TODO Auto-generated catch block
taSum.append("与服务器断开连接!");
try {
br.close();
ps.close();
s.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
//////////////////////////////////////////////////
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货