今天学习了老师讲的java的第一个小项目,是一个聊天程序雏形。
一个服务器端ChatServer.java,一个是客户端ChatClient.java
监听的是18881接口。
此小程序的截图如下,有兴趣的可以看一下
java聊天室
ChatServer.java的源代码如下:
[code lang="java"]
import java.io.*;
import java.net.*;
import java.util.*;

public class ChatServer
{
boolean started = false;
ServerSocket ss = null;

List<Client> clients = new ArrayList<Client>();

public static void main(String[] args){
new ChatServer().start();
}

public void start(){
try {
ss = new ServerSocket(18881);
started = true;
System.out.println("This is 18881 chatting room chatting history");
} catch (BindException b) {
System.out.println("端口使用中");
System.out.println("请关掉相关程序并重新运行服务器");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}

try {
int i = 1;
while(started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println(i + " chatter connected,it's IP:" + s.getInetAddress() + ",it's port:" + s.getPort());
i++;
//一个客户端启动一个对应的服务器线程
new Thread(c).start();
clients.add(c);
}
} catch (IOException e) {
System.out.println("a chatter out");
}finally {
try {
ss.close();
} catch (IOException i){
i.printStackTrace();
}
}
}

class Client implements Runnable {
private Socket s = null;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
public Client(Socket s){
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}

public void send(String str){
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
System.out.println("一个client退出了,我将它去除了");
}
}

public void run() {
Client c = null;
try{
while (bConnected) {
String str=dis.readUTF();
System.out.println(s.getPort() + ":" + str);
for(int i=0;i<clients.size();i++){
c=clients.get(i);
c.send(s.getPort() + ":" + str);
}
}
} catch (SocketException s) {
System.out.println("quit");
} catch (EOFException e){
System.out.println("a quit");
}catch (IOException e) {
System.out.println("a client quit");
} finally {
try {
if(dis != null) dis.close();
if(dos != null) dos.close();
if(s != null) s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
[/code]
ChatClient.java的源代码如下:
[code lang="java"]
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class ChatClient extends Frame
{
TextField tf;
TextArea ta;
Socket s = null;
DataOutputStream dos = null;
DataInputStream dis=null;
private boolean bConnected = false;
public static void main(String[] args){
new ChatClient().LaunchFrame();
}

public void LaunchFrame(){
setLocation(100,100);
setSize(400,300);
tf = new TextField();
ta = new TextArea();
add(tf,BorderLayout.SOUTH);
add(ta,BorderLayout.NORTH);
pack();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
disconnect();
System.exit(0);
}
});
tf.addActionListener(new TFListener());
setVisible(true);
connect();

new Thread(new RecvThread()).start();
}

private void connect(){
try
{
s = new Socket("127.0.0.1",18881);
dos = new DataOutputStream(s.getOutputStream());
dis = new DataInputStream(s.getInputStream());
ta.setText("-----welcome to 18881 chatting room-----" + '\n');

bConnected = true;
}
catch (IOException e)
{
e.printStackTrace();
}
}

public void disconnect(){
try{
dos.close();
s.close();
} catch (IOException i) {
i.printStackTrace();
}

}

class TFListener implements ActionListener
{
public void actionPerformed(ActionEvent e){
String str=tf.getText();
//ta.setText(str);
tf.setText("");
try {
dos.writeUTF(str);
dos.flush();
//System.out.println(dis.readUTF());
//dos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

private class RecvThread implements Runnable{
public void run(){
try {
while(bConnected){
String str = dis.readUTF();
ta.setText(ta.getText() + str + '\n');
}
} catch (SocketException s) {
System.out.println("退");
} catch (EOFException e){
System.out.println("退出");
}catch (IOException e) {
System.out.println("退出了");
}

}
}
}
[/code]

分类: Java

发表评论

电子邮件地址不会被公开。 必填项已用*标注