上海汽车2018校园招聘:求 做局域网聊天工具 和代码

来源:百度文库 编辑:神马品牌网 时间:2024/05/06 18:26:54
想自己做个 局域网聊天工具(类似于QQ的) 望 大哥门能提供代码 最好有详解谢谢

晚上来找我,加我QQ:452362812.

以前写过,但是功能较弱,给你粘过来参考一下:
这个是server的
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class sever extends JFrame{
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
public sever(){
super("Sever");
enterField=new JTextField();
enterField.setEditable(false);
enterField.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
sendData(e.getActionCommand());
}
});
Container container=getContentPane();
container.add(enterField,BorderLayout.NORTH);
displayArea=new JTextArea();
container.add(new JScrollPane(displayArea),BorderLayout.CENTER);
setSize(400,500);
setVisible(true);
}
public void run(){
try{
server=new ServerSocket(3200,3);
while(true){
waitForConnection();
getStreams();
processConnection();
closeConnection();
}
}catch(EOFException e){}
catch (IOException e){}
}
private void waitForConnection() throws IOException{
displayArea.setText("wait\n");
connection=server.accept();
displayArea.append("connect:"+connection.getInetAddress().getHostName());
}
private void getStreams() throws IOException{
output =new ObjectOutputStream(connection.getOutputStream());
output.flush();
input=new ObjectInputStream(connection.getInputStream());
displayArea.setText("\ngotIO\n");
}
private void processConnection() throws IOException{
String message="sever>>OK!";
output.writeObject(message);
output.flush();
enterField.setEditable(true);
do{
try{
message=(String)input.readObject();
displayArea.append("\n"+message);

}catch(ClassNotFoundException e){}
}while(!message.equals("client>>over"));
}
private void closeConnection() throws IOException{
displayArea.append("\nclose");
output.close();
input.close();
connection.close();
}
private void sendData(String message){
try{
output.writeObject("server>>"+message);
output.flush();
}catch(IOException e){}
}
public static void main(String[] args){
sever aa=new sever();
aa.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
aa.run();
}
}
这个是Client的
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class client extends JFrame{
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message;
private String chatServer;
private Socket client;
public client (){
super("client");
//chatServer=host;
enterField=new JTextField();
enterField.setEditable(false);
enterField.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
sendData(e.getActionCommand());
}
});
Container container=getContentPane();
container.add(enterField,BorderLayout.NORTH);
displayArea=new JTextArea();
container.add(new JScrollPane(displayArea),BorderLayout.CENTER);
setSize(400,500);
setVisible(true);
}
public void run(){
try{ connectToServer();
getStreams();
processConnection();
closeConection();
}catch(EOFException e){System.out.println("fuck");}
catch(IOException e){System.out.println("dkfj");}
}
private void connectToServer() throws IOException{
displayArea.append("connect\n");
client=new Socket(InetAddress.getByName("127.0.0.1"),609);

}
private void getStreams() throws IOException{
output =new ObjectOutputStream(client.getOutputStream());
output.flush();
input=new ObjectInputStream(client.getInputStream());
displayArea.setText("\ngotIO\n");
}
private void processConnection() throws IOException{
enterField.setEditable(true);
do{
try{
message=(String)input.readObject();
displayArea.append(message+"\n");
}catch(ClassNotFoundException e){}
}while(!message.equals("server>>over"));
}
private void closeConection() throws IOException{
displayArea.append("\nclose");
output.close();
input.close();
client.close();
}
private void sendData(String message){
try{
output.writeObject(message);
output.flush();
}catch(IOException e){}
}
public static void main(String[] args){
client aa=new client();
aa.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
aa.run();
}
}

Java的太复杂了。
建议你用VB吧!Winsock控件很不错的。
再加上Window API,一定别Java做的牛!
具体也可以参考www.vbgood.com.
我的E-mail:w_binglong@yahoo.com.cn