Java为pcDuino制做简单局域网聊天软件-Arduino中文社区 - Powered by Discuz! Archiver

Lily 发表于 2014-3-21 17:45

Java为pcDuino制做简单局域网聊天软件

本帖最后由 Lily 于 2014-3-21 17:53 编辑

一、运行效果运行服务端,修改客户端代码服务IP二、源代码服务端源代码?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.net.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class ChatServer extends Frame
{
TextArea ta = new TextArea();
public void launchFrame()
{
add(ta, BorderLayout.CENTER);
setBounds(0,0,200,300);
this.addWindowListener(
   new WindowAdapter()
   {
    public void windowClosing(WindowEvent e)
    {
   System.exit(0);
    }
   }
   );
setVisible(true);
}

ServerSocket server = null;
Collection cClient = new ArrayList();

public ChatServer(int port) throws Exception
{
server = new ServerSocket(port);
launchFrame();
}

public void startServer() throws Exception
{
while(true)
{
   Socket s = server.accept();
   cClient.add( new ClientConn(s) );
   ta.append("NEW-CLIENT " + s.getInetAddress() + ":" + s.getPort());
   ta.append("\n" + "CLIENTS-COUNT: " + cClient.size() + "\n\n");
}
}

class ClientConn implements Runnable
{
Socket s = null;
public ClientConn(Socket s)
{
   this.s = s;
   (new Thread(this)).start();
}

public void send(String str) throws IOException
{
   DataOutputStream dos = new DataOutputStream(s.getOutputStream());
   dos.writeUTF(str);
}

public void dispose()
{
   try {
    if (s != null) s.close();
    cClient.remove(this);
    ta.append("A client out! \n");
    ta.append("CLIENT-COUNT: " + cClient.size() + "\n\n");
   }
   catch (Exception e)
   {
    e.printStackTrace();
   }
}

public void run()
{
   try {

    DataInputStream dis = new DataInputStream(s.getInputStream());
    String str = dis.readUTF();
    while(str != null && str.length() !=0)
    {
   System.out.println(str);
   for(Iterator it = cClient.iterator(); it.hasNext(); )
   {
      ClientConn cc = (ClientConn)it.next();
      if(this != cc)
      {
       cc.send(str);
      }
   }
   str = dis.readUTF();
   //send(str);
    }
    this.dispose();
   }
   catch (Exception e)
   {
    System.out.println("client quit");
    this.dispose();
   }

}
}

public static void main(String[] args) throws Exception
{
ChatServer cs = new ChatServer(8888);
cs.startServer();
}
}




客户端源代码?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class ChatClient extends Frame
{
/**
   *
   */
private String ip="192.168.137.1";
private static final long serialVersionUID = 1L;
TextArea ta = new TextArea();
TextField tf = new TextField();
public void launchFrame() throws Exception
{
this.add(ta, BorderLayout.CENTER);
this.add(tf, BorderLayout.SOUTH);
tf.setText("客户端:");
tf.addActionListener(
   new ActionListener()
   {
    public void actionPerformed(ActionEvent ae)
    {
   try {
      String sSend = tf.getText();
      if(sSend.trim().length() == 0) return;
      ChatClient.this.send(sSend);
      tf.setText("客户端:");
      ta.append(sSend + "\n");
   }
   catch (Exception e) { e.printStackTrace(); }
    }
   }
   );

this.addWindowListener(
   new WindowAdapter()
   {
    public void windowClosing(WindowEvent e)
    {
   System.exit(0);
    }
   }
   );
setBounds(300,300,300,400);
setVisible(true);
tf.requestFocus();
}

Socket s = null;

public ChatClient() throws Exception
{
s = new Socket(ip, 8888);
launchFrame();
(new Thread(new ReceiveThread())).start();
}

public void send(String str) throws Exception
{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(str);
}

public void disconnect() throws Exception
{
s.close();
}

public static void main(String[] args) throws Exception
{
BufferedReader br = new BufferedReader (
      new InputStreamReader(System.in));
ChatClient cc = new ChatClient();
String str = br.readLine();
while(str != null && str.length() != 0)
{
   cc.send(str);
   str = br.readLine();
}
cc.disconnect();
}

class ReceiveThread implements Runnable
{
public void run()
{
   if(s == null) return;
   try {
    DataInputStream dis = new DataInputStream(s.getInputStream());
    String str = dis.readUTF();
    while (str != null && str.length() != 0)
    {
   //System.out.println(str);
   ChatClient.this.ta.append(str + "\n");
   str = dis.readUTF();
    }
   }
   catch (Exception e)
   {
    e.printStackTrace();
   }

}
}
}







kevinzhang19701 发表于 2014-4-21 13:18

本帖最后由 kevinzhang19701 于 2014-4-21 13:42 编辑

{:soso_e181:}{:soso_e179:}有意思滴试验

顺便请教一下Java的程序拿什么来编译呢?{:soso_e183:}
页: [1]
查看完整版本: Java为pcDuino制做简单局域网聊天软件