标签:clientserver eclipse exe java
先建两个Java Project项目,一个写Client,一个写Server端程序,程序大致为一个Server端建立监听某个端口,多个Client端可以连接,实现如下:
1. Server端
importjava.io.*;
importjava.net.*;
importjava.util.*;
publicclass SimpleChatServer {
ArrayList<PrintWriter>clientOutputStreams;
public static void main(String[]args){
new SimpleChatServer().go();
}
public class ClientHandler implementsRunnable{
BufferedReader reader;
Socket sock;
public ClientHandler(SocketclientSocket){
try{
sock = clientSocket;
InputStreamReader isReader =new InputStreamReader(sock.getInputStream());
reader = newBufferedReader(isReader);
}catch(Exception ex){
ex.printStackTrace();
}
}
public void run() {
String message;
try{
while((message =reader.readLine()) != null){
System.out.println("read " + message);
tellEveryone(message);
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public void tellEveryone(Stringmessage){
Iterator<PrintWriter> it =clientOutputStreams.iterator();
while(it.hasNext()){
try{
PrintWriter writer =(PrintWriter)it.next();
writer.println(message);
writer.flush();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
public void go(){
clientOutputStreams = newArrayList<PrintWriter>();
try{
ServerSocket serverSock = newServerSocket(2310);
while(true){
Socket clientSocket =serverSock.accept();
PrintWriter writer = newPrintWriter(clientSocket.getOutputStream());
clientOutputStreams.add(writer);
Thread t = new Thread(newClientHandler(clientSocket));
t.start();
System.out.println("got aconnection");
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}
2. Client端
importjava.io.*;
importjava.net.*;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.JOptionPane;
publicclass SimpleChatClient {
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;
static String clientName;
public static void main(String[] args){
SimpleChatClient client = newSimpleChatClient();
clientName = JOptionPane.showInputDialog("Pleaseinput the client name :");
client.go();
}
public void go() {
// build GUI
JFrame frame = new JFrame(clientName +"‘s Chat Client");
JPanel mainPanel = new JPanel();
incoming = new JTextArea(15,20);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);
JScrollPane qScroller = newJScrollPane(incoming);
qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing = new JTextField(20);
JButton sendButton = newJButton("Send");
sendButton.addActionListener(newSendButtonListener());
mainPanel.add(qScroller);
mainPanel.add(outgoing);
mainPanel.add(sendButton);
setUpNetworking();
Thread readerThread = new Thread(newIncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setSize(400,500);
frame.setVisible(true);
} // close go
private void setUpNetworking() {
try {
sock = newSocket("127.0.0.1", 2310);
InputStreamReader streamReader =new InputStreamReader(sock.getInputStream());
reader = newBufferedReader(streamReader);
writer = newPrintWriter(sock.getOutputStream());
System.out.println("networkingestablished");
} catch(IOException ex) {
ex.printStackTrace();
}
} // close setUpNetworking
public class SendButtonListener implementsActionListener {
public void actionPerformed(ActionEventev) {
try {
writer.println(clientName+" says:\n"+outgoing.getText());
writer.flush();
} catch(Exception ex) {
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}
} //close SendButtonListener inner class
public class IncomingReader implementsRunnable {
public void run() {
String message = null;
try {
while ((message =reader.readLine()) != null) {
System.out.println("read " + message);
incoming.append(message +"\n");
} // close while
} catch(Exception ex){ex.printStackTrace();}
} // close run
} // close inner class
}
下面以Server端生成exe为例进行讲解:
1.导出jar文件
选中你的项目---然后右击弹出选项列表---选择Export---Java---jarfile---next---browser(选择jar的存放路径)---next---next(不管太多继续下一步)---browser(选择你程序的主类--OK)---finish
2.安装exe4j
利用exe4j这个软件转换jar文件为exe文件,exe4j下载地址:http://www.ej-technologies.com/download/exe4j/files.html.
3.exe4j安装完成后
1.运行后直接点Next.
2.选择第二项“JAR in EXE mode”,之后点击Next.
3.在General---Short name of your application框中输入你的应用程序的名字(随便即可),在Directories---Outputdirectory框中输入生成exe后的保存路径,然后Next.
4.在Excutable name框中输入将要生成的exe的名字,“Icon File”是应用程序显示的小图标,不选也行.继续Next.
5.必须先配置Class-Path,点击+号添加文件,即jar文件,如果有用到其它资源需要把资源一起添加进去.
6.General --- Main Class选择主类.
7.Java Version --- Minimum version(低版本必须填写),Maximumversion(高版本可以为空),如果都有填写那么高版本的值必须大于低版本的值.
8.勾选Allow JREs with a beta version number以及Only allow JDKs andno JREs.接着Next.到这一步你可以直接finish.这样就把jar转换成exe文件了.你可以点击Click Here to Start the Application按钮测试生成的文件.
Client端生成exe步骤同上。
标签:clientserver eclipse exe java
原文地址:http://blog.csdn.net/woody891/article/details/44809287