请教各位高人:
我有一段用socket实现的c/s聊天程序,但只是文字的。我想把它改造成用socket在两个线程之间不仅能传输文字,还能传输文件。现无从下手,请指教。源程序如下,仅供参考。谢谢!//服务器端的
public class ChatServer {
/* m_threads是一个Vector静态变量,维护所有Server方的 ServerThread对象,
* 通过该变量能向所有加入聊天室的聊天者ChatApplet(ChatApplication)广播信息,撤消退出的
* 聊天者。
* 聊天服务者ChatServer的主方法。该方法监听聊天者ChatApplet(ChatApplication)的请求,
* 并为新连接的聊天者创建一个服务线程。
*/
public static void main(String args[]) {
ServerSocket socket = null;
Vector m_threads = new Vector();
System.out.println("listen...");
try {
//设置Server监听端口号为5555, 这个数字必须
//和程序聊天者ChatApplet(ChatApplication)中的port参数一致。
socket = new ServerSocket(5555);
} catch (Exception e) {
System.out.println("new ServerSocket()failed!");
return;
}
try {
int nid = 0;
while (true) {
//监听是否有新聊天者ChatApplet(ChatApplication)连接到聊天Server,
//线程运行到该语句会封锁,直到有新的连接产生。
Socket s = socket.accept();
System.out.println("accepted");
//创建一个新的ServerThread.
ServerThread st = new ServerThread(s, m_threads);
//为该线程设置一个ID号。
st.setID(nid++);
//将该线程加入到m_threads Vector中。
m_threads.addElement(st);
//启动服务线程。
new Thread(st).start();
//通知所有ChatApplet(ChatApplication)有一个新的网友加入。
for (int i = 0; i < m_threads.size(); i++) {
ServerThread st1 = (ServerThread) m_threads.elementAt(i);
st1.write("<#>welcome " + st.getID() +
" to enter chatroom!");
}
System.out.println("Listen again...");
}
} catch (Exception e) {
System.out.println("Server is down...");
} }
}
/*
* 监听线程,监听对应的Chat Applet(ChatApplication)是否有信息传来。
*/
class ServerThread implements Runnable {
Vector m_threads;
Socket m_socket = null;
DataInputStream m_in = null;
DataOutputStream m_out = null;
int m_nid;
//初始化该线程。
public ServerThread(Socket s, Vector threads) {
m_socket = s;
m_threads = threads;
try { //构造数据输入、输出流对象
m_in = new DataInputStream(m_socket.getInputStream());
m_out = new DataOutputStream(m_socket.getOutputStream());
} catch (Exception e) {
}
} public void run() { //线程的执行体。
System.out.println("thread is running");
try {
while (true) {
String s = m_in.readUTF();
if (s == null) {
break;
}
if (s.trim().equals("leave")) {
for (int i = 0; i < m_threads.size(); i++) {
ServerThread st = (ServerThread) m_threads.elementAt(i);
st.write("*** " + getID() + " leave..." + "***");
m_threads.removeElementAt(getID());
}
} else {
//向所有ChatApplet(ChatApplication)广播该信息。
for (int i = 0; i < m_threads.size(); i++) {
ServerThread st = (ServerThread) m_threads.elementAt(i);
st.write("<" + getID() + ">" + s);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
//从m_threads Vector中删除该线程,表示该线程已经离开聊天室。
m_threads.removeElement(this);
try {
m_socket.close();
} catch (Exception e) {}
} //将msg送回对应的Applet
public void write(String msg) {
synchronized (m_out) {
try {
m_out.writeUTF(msg);
} catch (IOException e) {}
}
} public int getID() { //获得该线程的ID.
return m_nid;
} public void setID(int nid) // //设置线程的ID. {
m_nid = nid;
}
}
//客户端的
public class ChatApplication1 extends JFrame{
...
DataInputStream m_in=null; //消息输入流
DataOutputStream m_out=null; //消息输出流
Socket m_socket;
/**
* Construct and show the application.
*/
public ChatApplication1() {
//界面布局,设置监听接口
...
//初始化Application,并连接到聊天服务者
try {
//获取application 的URL ,即聊天服务者地址
URL url=new URL("http://localhost");
//获取服务器IP地址
InetAddress inetaddr = InetAddress.getByName(url.getHost());
//屏幕显示服务器IP地址、通信协议
System.out.println("Server:" + inetaddr + " " + url.getHost() + " " +
url.getProtocol());
//创建与服务器IP地址连接的套接口,5555是聊天服务者套接口端口
m_socket = new Socket(inetaddr, 5555);
//在套接口上建立输入流
m_in = new DataInputStream(m_socket.getInputStream());
//在套接口上建立输出流
m_out = new DataOutputStream(m_socket.getOutputStream());
} catch (Exception e) {
System.out.println("Error:" + e);
} try {
while (true) {
//聊天者监听对应服务线程发来的消息,它将封锁在该语句中,
//直到消息到来。
String s = m_in.readUTF(); //读一个UTF格式字符串。
if (s != null) {
//将消息显示在信息显示窗口中。
m_TextArea.append(s + "\n");
}
}
} catch (Exception e) {
m_TextArea.append("Network problem or Server down.\n");
m_TextField.setVisible(false);
} } /**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args) {
... } private void jbInit() throws Exception {
//界面布局
... } ...
}class quit_actionAdapter implements ActionListener {
private ChatApplication1 adaptee;
quit_actionAdapter(ChatApplication1 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.quit_actionPerformed(e);
}
}
...
我有一段用socket实现的c/s聊天程序,但只是文字的。我想把它改造成用socket在两个线程之间不仅能传输文字,还能传输文件。现无从下手,请指教。源程序如下,仅供参考。谢谢!//服务器端的
public class ChatServer {
/* m_threads是一个Vector静态变量,维护所有Server方的 ServerThread对象,
* 通过该变量能向所有加入聊天室的聊天者ChatApplet(ChatApplication)广播信息,撤消退出的
* 聊天者。
* 聊天服务者ChatServer的主方法。该方法监听聊天者ChatApplet(ChatApplication)的请求,
* 并为新连接的聊天者创建一个服务线程。
*/
public static void main(String args[]) {
ServerSocket socket = null;
Vector m_threads = new Vector();
System.out.println("listen...");
try {
//设置Server监听端口号为5555, 这个数字必须
//和程序聊天者ChatApplet(ChatApplication)中的port参数一致。
socket = new ServerSocket(5555);
} catch (Exception e) {
System.out.println("new ServerSocket()failed!");
return;
}
try {
int nid = 0;
while (true) {
//监听是否有新聊天者ChatApplet(ChatApplication)连接到聊天Server,
//线程运行到该语句会封锁,直到有新的连接产生。
Socket s = socket.accept();
System.out.println("accepted");
//创建一个新的ServerThread.
ServerThread st = new ServerThread(s, m_threads);
//为该线程设置一个ID号。
st.setID(nid++);
//将该线程加入到m_threads Vector中。
m_threads.addElement(st);
//启动服务线程。
new Thread(st).start();
//通知所有ChatApplet(ChatApplication)有一个新的网友加入。
for (int i = 0; i < m_threads.size(); i++) {
ServerThread st1 = (ServerThread) m_threads.elementAt(i);
st1.write("<#>welcome " + st.getID() +
" to enter chatroom!");
}
System.out.println("Listen again...");
}
} catch (Exception e) {
System.out.println("Server is down...");
} }
}
/*
* 监听线程,监听对应的Chat Applet(ChatApplication)是否有信息传来。
*/
class ServerThread implements Runnable {
Vector m_threads;
Socket m_socket = null;
DataInputStream m_in = null;
DataOutputStream m_out = null;
int m_nid;
//初始化该线程。
public ServerThread(Socket s, Vector threads) {
m_socket = s;
m_threads = threads;
try { //构造数据输入、输出流对象
m_in = new DataInputStream(m_socket.getInputStream());
m_out = new DataOutputStream(m_socket.getOutputStream());
} catch (Exception e) {
}
} public void run() { //线程的执行体。
System.out.println("thread is running");
try {
while (true) {
String s = m_in.readUTF();
if (s == null) {
break;
}
if (s.trim().equals("leave")) {
for (int i = 0; i < m_threads.size(); i++) {
ServerThread st = (ServerThread) m_threads.elementAt(i);
st.write("*** " + getID() + " leave..." + "***");
m_threads.removeElementAt(getID());
}
} else {
//向所有ChatApplet(ChatApplication)广播该信息。
for (int i = 0; i < m_threads.size(); i++) {
ServerThread st = (ServerThread) m_threads.elementAt(i);
st.write("<" + getID() + ">" + s);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
//从m_threads Vector中删除该线程,表示该线程已经离开聊天室。
m_threads.removeElement(this);
try {
m_socket.close();
} catch (Exception e) {}
} //将msg送回对应的Applet
public void write(String msg) {
synchronized (m_out) {
try {
m_out.writeUTF(msg);
} catch (IOException e) {}
}
} public int getID() { //获得该线程的ID.
return m_nid;
} public void setID(int nid) // //设置线程的ID. {
m_nid = nid;
}
}
//客户端的
public class ChatApplication1 extends JFrame{
...
DataInputStream m_in=null; //消息输入流
DataOutputStream m_out=null; //消息输出流
Socket m_socket;
/**
* Construct and show the application.
*/
public ChatApplication1() {
//界面布局,设置监听接口
...
//初始化Application,并连接到聊天服务者
try {
//获取application 的URL ,即聊天服务者地址
URL url=new URL("http://localhost");
//获取服务器IP地址
InetAddress inetaddr = InetAddress.getByName(url.getHost());
//屏幕显示服务器IP地址、通信协议
System.out.println("Server:" + inetaddr + " " + url.getHost() + " " +
url.getProtocol());
//创建与服务器IP地址连接的套接口,5555是聊天服务者套接口端口
m_socket = new Socket(inetaddr, 5555);
//在套接口上建立输入流
m_in = new DataInputStream(m_socket.getInputStream());
//在套接口上建立输出流
m_out = new DataOutputStream(m_socket.getOutputStream());
} catch (Exception e) {
System.out.println("Error:" + e);
} try {
while (true) {
//聊天者监听对应服务线程发来的消息,它将封锁在该语句中,
//直到消息到来。
String s = m_in.readUTF(); //读一个UTF格式字符串。
if (s != null) {
//将消息显示在信息显示窗口中。
m_TextArea.append(s + "\n");
}
}
} catch (Exception e) {
m_TextArea.append("Network problem or Server down.\n");
m_TextField.setVisible(false);
} } /**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args) {
... } private void jbInit() throws Exception {
//界面布局
... } ...
}class quit_actionAdapter implements ActionListener {
private ChatApplication1 adaptee;
quit_actionAdapter(ChatApplication1 adaptee) {
this.adaptee = adaptee;
} public void actionPerformed(ActionEvent e) {
adaptee.quit_actionPerformed(e);
}
}
...
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货