运用socket编写一个JAVA程序

QQ聊天之类的……... QQ聊天之类的…… 展开
 我来答
heartfleet
2011-05-03 · TA获得超过248个赞
知道答主
回答量:135
采纳率:0%
帮助的人:118万
展开全部
/*服务器端程序:*/
mport java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

class ChatServer extends JFrame{
private JTextField inputBox;
private JTextArea outFrame;
private ObjectOutputStream outputS;
private ObjectInputStream inputS;
private ServerSocket toserver;
private Socket server;
private String message = "";
private int counter = 1;

ChatServer(){
super("Server");

Container container = getContentPane();
inputBox = new JTextField();
inputBox.setEditable(false);
//输出
inputBox.addActionListener(
//监听
new ActionListener(){
public void actionPerformed(ActionEvent ev)
{
sendMsg(ev.getActionCommand());
inputBox.setText(" ");
}
});
container.add(inputBox,BorderLayout.NORTH);
//输出框
outFrame = new JTextArea();
container.add(new JScrollPane(outFrame),
BorderLayout.CENTER);
setSize(208,160);
setVisible(true);
}

public void connectServer(){

try{
//创建一个ServerSocket
toserver = new ServerSocket(4000);
while(true){
//等待连接
wait1Connection();
//获取输出流
getStreams();
//处理连接
processConnection();
//关闭连接
closeConnection();
++counter;

}
}
catch(EOFException eofException){
System.out.println("Client terminated connection");
}
catch(IOException ee){
ee.printStackTrace();

}
}
private void wait1Connection() throws IOException
{
outFrame.setText("等待连接....\n");
server = toserver.accept();
outFrame.append("连接 " + counter +
" 来自于:" + server.getInetAddress().getHostName());
}
private void getStreams() throws IOException
{
outputS = new ObjectOutputStream(server.getOutputStream());
outputS.flush();
inputS = new ObjectInputStream(server.getInputStream());
outFrame.append("\n成功建立连接。\n");
}
//处理客户端连接:
private void processConnection() throws IOException
{
//连接成功
message = "成功连接到-->>^_^小粉条";
outputS.writeObject(message);
outputS.flush();//刷新输出流
//输入框
inputBox.setEditable(true);
//处理来自客户端的消息
do{
//读取消息;
try{
message = (String) inputS.readObject();
outFrame.append("\n" + message);

outFrame.setCaretPosition(outFrame.getText().length());
}
catch(ClassNotFoundException ex){
outFrame.append("\n UnKown object type received");
}
}
while(!message.equals("一肩秋色>> TERMINATE"));

}
private void closeConnection() throws IOException
{
outFrame.append("\n User terminated connction");
inputBox.setEditable(true);
outputS.close();
inputS.close();
server.close();
}
//向客户端发送消息
private void sendMsg(String message)
{
try{
outputS.writeObject("^_^小粉条>> " + message);
outputS.flush();
outFrame.append("\n^_^小粉条>>" + message);
}catch(IOException em){
outFrame.append("\nError writing object");
}

}
}
public class chat {

public static void main(String[] args) {
// TODO code application logic here
ChatServer process = new ChatServer();
process.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
process.connectServer();
}
}

/*客户端程序:*/
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

class CClient extends JFrame{
private JTextField inputBox;
private JTextArea outFrame;
private ObjectOutputStream outputS;
private ObjectInputStream inputS;
private String message = "";
private String ChatServer;
private Socket toclient;
CClient(String srhost)
{
super("Client");
ChatServer = srhost;
//设置客户端连接的服务器
Container container = getContentPane();
inputBox = new JTextField();
//建立输入框
inputBox.setEditable(false);
inputBox.addActionListener(
//监听
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
sendMsg(e.getActionCommand());
inputBox.setText(" ");
}
}
);
container.add(inputBox,BorderLayout.NORTH);
//输出框
outFrame = new JTextArea();
container.add(new JScrollPane(outFrame),
BorderLayout.CENTER);
setSize(208,160);
setVisible(true);
}
public void connectClient()
{
try{

//用于连接的Socket
connect1Server();
//得到输入输出流
getStreams();
//处理连接
processConnection();
//关闭连接
closeConnection();
}
catch(EOFException eofException){
System.out.println("Client terminated connection");
}
catch(IOException ee){
ee.printStackTrace();

}
}
//捕获异常
private void getStreams() throws IOException
{
outputS = new ObjectOutputStream(toclient.getOutputStream());
outputS.flush();
inputS = new ObjectInputStream(toclient.getInputStream());
outFrame.append("\n成功建立连接。\n");
}
private void connect1Server() throws IOException
{
outFrame.setText("连接中....\n");
toclient = new Socket(InetAddress.getByName(ChatServer),4000);
outFrame.append("连接至:" + toclient.getInetAddress().getHostName());
}
private void processConnection() throws IOException
{

inputBox.setEditable(true);
//处理来自客户端的消息
do{
//读取消息;
try{
message = (String) inputS.readObject();
outFrame.append("\n" + message);

outFrame.setCaretPosition(outFrame.getText().length());
}
catch(ClassNotFoundException ex){
outFrame.append("\n UnKown object type received");
}
}
while(!message.equals("^_^小粉条>> TERMINATE"));

}
private void closeConnection() throws IOException
{
outFrame.append("\n 关闭连接");
inputBox.setEditable(false);
outputS.close();
inputS.close();
toclient.close();
}
//向服务器端发送消息
private void sendMsg(String message)
{
try{
outputS.writeObject("一肩秋色>> " + message);
outputS.flush();
outFrame.append("\n一肩秋色>>" + message);
}catch(IOException em){
outFrame.append("\nError writing object");
}
}

}
public class ChatClient {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
CClient beginning;
if(args.length == 0)
beginning = new CClient("192.168.1.100");//连接到IP
else
beginning = new CClient(args[0]);
beginning.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
beginning.connectClient();
}

}
//无聊写的简单Socket程序,你把里面的名字改下应该就可以用
追问
貌似……这个……百度出来的
周是周树人的周
2011-05-03
知道答主
回答量:56
采纳率:100%
帮助的人:10.2万
展开全部
不懂啊
追问
额……我也是
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式