java仿qq聊天工具 用 MyEclipse 能运行的
展开全部
这种例子很多的:三个类。
第一个类:
package chat;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ServerManage extends Thread {//定义线程
private JFrame jFrame;
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private Socket connection;
//建立GUI
public ServerManage(Socket socket){
connection=socket;
}//ServerManage
private void getStreams() throws IOException
{
output = new ObjectOutputStream(connection.getOutputStream() );
output.flush();
input = new ObjectInputStream(connection.getInputStream() );
displayArea.append( "\n客户端已联接!\n" );
} //getStreams
private void processConnection() throws IOException //处理联接方法体
{
String message = "联接成功,可以会话!";//发消息给CLIENT,通知联接以经建立
output.writeObject( message );
output.flush();
enterField.setEnabled( true );
do {
try{
message = ( String ) input.readObject();
displayArea.append( "\n" + message );
displayArea.setCaretPosition(displayArea.getText().length() );
}
catch(Exception e){
displayArea.setText("联接关闭.....");
}
} while ( !message.equals( "客户机: bay" ) );//设定终止口令
}
private void closeConnection() throws IOException //关闭联接方法体
{
enterField.setEnabled( false );
output.close();
input.close();
connection.close();
}//closeConnection
private void sendData( String message ) //发送消息方法
{
try {
output.writeObject("\n");
output.writeObject( "主机: "+message );
// output.writeObject( message );
output.flush();
displayArea.append( "\n我: "+message );
displayArea.setCaretPosition(displayArea.getText().length() );
// displayArea.append( message );
enterField.setText("");//清除输入行内容
}
catch ( IOException ioException ) {
displayArea.append( "\n无法识别......" );//异常处理
}
}
public void run(){
jFrame=new JFrame("与"+ connection.getInetAddress()+"对话");
Container container = jFrame.getContentPane();
enterField = new JTextField();//输入行
enterField.setEnabled(true);
enterField.addActionListener(new ActionListener() {
public void actionPerformed( ActionEvent event ){
// sendData(enterField.getText());//获取文本框中字符串
sendData(event.getActionCommand());
}
}
); //加入事件
displayArea = new JTextArea(10,10);//消息显示区
displayArea.setBackground(Color.lightGray);
// displayArea.setForeground (Color.green);
Label label=new Label("在下面区域输入要发送的内容,按回车键发送...");
container.add( new JScrollPane( displayArea ),BorderLayout.NORTH );
container.add(label,BorderLayout.CENTER);
container.add(enterField, BorderLayout.SOUTH );
jFrame.setSize( 300, 300);//设定窗体大小
jFrame.setVisible( true );//窗体可见
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
try {
while ( true ) {
getStreams();
processConnection();
closeConnection();
}
}
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
}
////////////////第二个类
package chat;
import java.net.*;
import javax.swing.*;
public class Server extends JFrame {
private static final long serialVersionUID = 1L;
private int port=1234;//设定通讯端口为1234
private ServerSocket server;
int n=0;
public Server(){
try{
server = new ServerSocket( port, 100 );//最多100个对话
while(n<100){
ServerManage serverManage=new ServerManage(server.accept());
serverManage.start();
n++;
}
}
catch(Exception e){
System.out.println(e);
}
}
public static void main( String args[] )
{
new Server();
}
}
////第三个类
package chat;
//Client.java
//导入有关包
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.*;
public class Client extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message =null;
private String chatServer;
private Socket client;
//建立GUI
public Client( String host )//以主机IP或主机名为参数
{
super( "客户端tip" );
chatServer = host;
Container container = getContentPane();
enterField = new JTextField();//输入行
enterField.setEnabled( true );
enterField.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event ){
sendData( event.getActionCommand() );
}
}
);//加入方法(发送消息给主机)
displayArea = new JTextArea(10,10);//输出区
Label label=new Label("在下面区域输入要发送的内容,按回车键发送...");
container.add( new JScrollPane( displayArea ),BorderLayout.NORTH );
displayArea.setBackground(Color.lightGray);
container.add(label,BorderLayout.CENTER);
container.add( enterField, BorderLayout.SOUTH );
setSize( 300, 300 );//设定窗口大小
setVisible( true );//设为可见
}
public void runClient()
{
try {
connectToServer();//建立SOCKET联接
getStreams();//得到输入输出流
processConnection();//处理联接
closeConnection();//关闭联接
}
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
private void getStreams() throws IOException{
output = new ObjectOutputStream(client.getOutputStream() );
output.flush();
input = new ObjectInputStream( client.getInputStream() );
}
private void connectToServer() throws IOException{ // 建立SOCKET联接方法体
displayArea.setText( "尝试联接......\n" );
//以主机名或IP及端口号为参数建立联接
client = new Socket(InetAddress.getByName( chatServer ), 1234 );
}
private void processConnection() throws IOException //处理联接方法体
{
enterField.setEnabled( true );//输入行变成可编辑
do {
try {
message = ( String ) input.readObject();
displayArea.append(message );
displayArea.setCaretPosition( displayArea.getText().length() );
}
catch(Exception e){
displayArea.setText("联接关闭.......");//异常处理
}
} while ( !message.equals( "主机: bay" ) );//设定终止口令
}
private void closeConnection() throws IOException //关闭联接方法体
{
displayArea.append( "\n联接关闭......." );
output.close();
input.close();
client.close();
}
private void sendData( String message )//发送消息方法体
{
try {
// output.writeObject("\n");
output.writeObject( "客户机: "+message);
output.flush();
enterField.setText("");//清除输入行内容
displayArea.append( "\n我: "+message);
displayArea.setCaretPosition(displayArea.getText().length() );
}
catch ( IOException ioException ) {
displayArea.append( "\n无法识别......." );
}
}
public static void main( String args[] )
{
Client client = null;
String f=JOptionPane.showInputDialog(null,"请输入您要联接的主机IP或主机名:\n");
if ( args.length == 0 )
{client = new Client( f );}
client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );//关闭窗口方法
client.runClient();
}
}
先运行Server类,再运行Client类
第一个类:
package chat;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ServerManage extends Thread {//定义线程
private JFrame jFrame;
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private Socket connection;
//建立GUI
public ServerManage(Socket socket){
connection=socket;
}//ServerManage
private void getStreams() throws IOException
{
output = new ObjectOutputStream(connection.getOutputStream() );
output.flush();
input = new ObjectInputStream(connection.getInputStream() );
displayArea.append( "\n客户端已联接!\n" );
} //getStreams
private void processConnection() throws IOException //处理联接方法体
{
String message = "联接成功,可以会话!";//发消息给CLIENT,通知联接以经建立
output.writeObject( message );
output.flush();
enterField.setEnabled( true );
do {
try{
message = ( String ) input.readObject();
displayArea.append( "\n" + message );
displayArea.setCaretPosition(displayArea.getText().length() );
}
catch(Exception e){
displayArea.setText("联接关闭.....");
}
} while ( !message.equals( "客户机: bay" ) );//设定终止口令
}
private void closeConnection() throws IOException //关闭联接方法体
{
enterField.setEnabled( false );
output.close();
input.close();
connection.close();
}//closeConnection
private void sendData( String message ) //发送消息方法
{
try {
output.writeObject("\n");
output.writeObject( "主机: "+message );
// output.writeObject( message );
output.flush();
displayArea.append( "\n我: "+message );
displayArea.setCaretPosition(displayArea.getText().length() );
// displayArea.append( message );
enterField.setText("");//清除输入行内容
}
catch ( IOException ioException ) {
displayArea.append( "\n无法识别......" );//异常处理
}
}
public void run(){
jFrame=new JFrame("与"+ connection.getInetAddress()+"对话");
Container container = jFrame.getContentPane();
enterField = new JTextField();//输入行
enterField.setEnabled(true);
enterField.addActionListener(new ActionListener() {
public void actionPerformed( ActionEvent event ){
// sendData(enterField.getText());//获取文本框中字符串
sendData(event.getActionCommand());
}
}
); //加入事件
displayArea = new JTextArea(10,10);//消息显示区
displayArea.setBackground(Color.lightGray);
// displayArea.setForeground (Color.green);
Label label=new Label("在下面区域输入要发送的内容,按回车键发送...");
container.add( new JScrollPane( displayArea ),BorderLayout.NORTH );
container.add(label,BorderLayout.CENTER);
container.add(enterField, BorderLayout.SOUTH );
jFrame.setSize( 300, 300);//设定窗体大小
jFrame.setVisible( true );//窗体可见
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
try {
while ( true ) {
getStreams();
processConnection();
closeConnection();
}
}
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
}
////////////////第二个类
package chat;
import java.net.*;
import javax.swing.*;
public class Server extends JFrame {
private static final long serialVersionUID = 1L;
private int port=1234;//设定通讯端口为1234
private ServerSocket server;
int n=0;
public Server(){
try{
server = new ServerSocket( port, 100 );//最多100个对话
while(n<100){
ServerManage serverManage=new ServerManage(server.accept());
serverManage.start();
n++;
}
}
catch(Exception e){
System.out.println(e);
}
}
public static void main( String args[] )
{
new Server();
}
}
////第三个类
package chat;
//Client.java
//导入有关包
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.*;
public class Client extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message =null;
private String chatServer;
private Socket client;
//建立GUI
public Client( String host )//以主机IP或主机名为参数
{
super( "客户端tip" );
chatServer = host;
Container container = getContentPane();
enterField = new JTextField();//输入行
enterField.setEnabled( true );
enterField.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event ){
sendData( event.getActionCommand() );
}
}
);//加入方法(发送消息给主机)
displayArea = new JTextArea(10,10);//输出区
Label label=new Label("在下面区域输入要发送的内容,按回车键发送...");
container.add( new JScrollPane( displayArea ),BorderLayout.NORTH );
displayArea.setBackground(Color.lightGray);
container.add(label,BorderLayout.CENTER);
container.add( enterField, BorderLayout.SOUTH );
setSize( 300, 300 );//设定窗口大小
setVisible( true );//设为可见
}
public void runClient()
{
try {
connectToServer();//建立SOCKET联接
getStreams();//得到输入输出流
processConnection();//处理联接
closeConnection();//关闭联接
}
catch ( IOException ioException ) {
ioException.printStackTrace();
}
}
private void getStreams() throws IOException{
output = new ObjectOutputStream(client.getOutputStream() );
output.flush();
input = new ObjectInputStream( client.getInputStream() );
}
private void connectToServer() throws IOException{ // 建立SOCKET联接方法体
displayArea.setText( "尝试联接......\n" );
//以主机名或IP及端口号为参数建立联接
client = new Socket(InetAddress.getByName( chatServer ), 1234 );
}
private void processConnection() throws IOException //处理联接方法体
{
enterField.setEnabled( true );//输入行变成可编辑
do {
try {
message = ( String ) input.readObject();
displayArea.append(message );
displayArea.setCaretPosition( displayArea.getText().length() );
}
catch(Exception e){
displayArea.setText("联接关闭.......");//异常处理
}
} while ( !message.equals( "主机: bay" ) );//设定终止口令
}
private void closeConnection() throws IOException //关闭联接方法体
{
displayArea.append( "\n联接关闭......." );
output.close();
input.close();
client.close();
}
private void sendData( String message )//发送消息方法体
{
try {
// output.writeObject("\n");
output.writeObject( "客户机: "+message);
output.flush();
enterField.setText("");//清除输入行内容
displayArea.append( "\n我: "+message);
displayArea.setCaretPosition(displayArea.getText().length() );
}
catch ( IOException ioException ) {
displayArea.append( "\n无法识别......." );
}
}
public static void main( String args[] )
{
Client client = null;
String f=JOptionPane.showInputDialog(null,"请输入您要联接的主机IP或主机名:\n");
if ( args.length == 0 )
{client = new Client( f );}
client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );//关闭窗口方法
client.runClient();
}
}
先运行Server类,再运行Client类
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
自己找个socket框架学习一下,别人写的那个什么qq的都是垃圾,可以看看mina之类框架,很好很强大。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询