用Socket监听有没有其他Socket请求连接,
如果不用多线程,
一开始监听就堵塞,
整个UI就死了,
只有用多线程才能实现在监听的同时还可以操作UI吧?
具体应该怎么实现呢?
------解决方案--------------------
http://blog.csdn.net/mhapdream/article/details/8929253 Socket:java与C之间的文件传送<1>(C语言之间的通信)
http://blog.csdn.net/mhapdream/article/details/8929457 Socket:java与C之间的文件传送<2>(JAVA语言之间的通信)
http://blog.csdn.net/mhapdream/article/details/8929590 Socket:java与C之间的文件传送<3>(JAVA与C通信需要注意的地方)
http://blog.csdn.net/mhapdream/article/details/8929638 Socket:java与C之间的文件传送<4>(JAVA与C通信)
------解决方案--------------------
网络连接 和UI 分开服务
------解决方案--------------------
各自用独立的类实现,彼此之间公布接口进行调用。
socket部分用多线程控制。
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
class ClientSocketListener extends Thread
{
private Socket socket;
private BufferedReader br;
ServerFrame serverFrame;
ClientSocketListener(Socket socket,ServerFrame serverFrame)
throws Exception
{
this.socket = socket;
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
this.serverFrame = serverFrame;
}
public void run()
{
String s = null;
try
{
while ( (s = br.readLine())!=null)
{
//向其他客户端发送
//ServerSocketThread.SendMsg(s);
//显示在Server的界面上
serverFrame.printServerMsg(s+"\r\n");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
class ServerSocketThread implements Runnable
{
ServerSocket serverSocket;
ServerFrame serverFrame;
static private ArrayList<PrintStream> sClientList = new ArrayList<PrintStream>();
static private ServerSocketThread serverSocketThread;
static public ServerSocketThread makeServerSocketThread(ServerFrame serverFrame)