您的位置:首页 > 理论基础 > 计算机网络

Java网络编程例-Socket

2011-12-20 20:49 309 查看
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class Server {
ServerSocket serverSocket=null;
public Server(){
try{
//注意,请下一次运行前关闭所有程序,否则端口号被JVM占用,将出错
serverSocket=new ServerSocket(4115);
}catch (Exception e) {
e.printStackTrace();
}
}
public void ready(){
Socket you=null;
while(true){
try{
you=serverSocket.accept(); //等待
if(you!=null)
new ServerThread(you).start(); //为每一个客户建立一个处理线程
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
continue;
}

}
}

public static void main(String[] args) {
Server server=new Server();
//在另一个线程里启动Client
Thread thread=new Thread(
new Runnable(){
public void run() {
// TODO Auto-generated method stub
Client client=new Client();
//try{Thread.sleep(1000);}catch (Exception e) {
// TODO: handle exception
//}
}
});
thread.start();
server.ready();

}
class ServerThread extends Thread{
Socket socket;
DataOutputStream out=null;
DataInputStream in=null;
String s=null;
public ServerThread(Socket t) {
// TODO Auto-generated constructor stub
socket=t;
try{
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void run() {
while(true){
try{
String s=in.readUTF();
System.out.println("服务器端收到:"+s);
String send="服务器发送当前服务器时间:"+new Date();
out.writeUTF(send);
}catch (Exception e) {
// TODO: handle exception
System.out.println("客户离开");
break;
}
}
}
}
}


服务器端程序与客户端程序,直接运行Server.java

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Client extends JFrame
implements Runnable,ActionListener{
Socket socket=null;
JButton connBtn,sendBtn;
JTextField input;
JTextArea showArea;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread=null;
boolean threadB=true;
public Client(){
socket=new Socket();
connBtn=new JButton("连接服务器");

sendBtn=new JButton("发送");
sendBtn.setEnabled(false); //未连接之前不可用
showArea=new JTextArea(8,18);
input=new JTextField(12);
Container container=getContentPane();
container.setLayout(new FlowLayout());
container.add(connBtn);
container.add(input);
container.add(sendBtn);
container.add(new JScrollPane(showArea));
sendBtn.addActionListener(this);
connBtn.addActionListener(this);

thread=new Thread(this);  //创建线程
setBounds(100, 100, 360, 310);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void run() {
// TODO Auto-generated method stub
while(threadB){    //探测服务器端是否发来数据
try{
String s=in.readUTF();//取到数据之前堵塞
showArea.append("接收到:"+s+"\n");
showArea.setCaretPosition(showArea.getText().length());
}catch (Exception e) {
// TODO: handle exception
showArea.append("连接已断开");
sendBtn.setEnabled(false);
break; //终止线程
}
}
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==connBtn){  //连接服务器的按钮
try{
if(!socket.isConnected()){
InetAddress address=InetAddress.getByName("127.0.0.1");
InetSocketAddress socketAddress=new InetSocketAddress(address,4115); //端口号
socket.connect(socketAddress); //连接服务器
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
sendBtn.setEnabled(true);  //发送按钮可用
thread.start();         //线程启动,用于接收服务器端发来的消息并显示

}
}catch (Exception ex) {
// TODO: handle exception
ex.printStackTrace();
socket=new Socket();
}

}else if(e.getSource()==sendBtn){
try{
String s=input.getText();
out.writeUTF(s);
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
Client client=new Client();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: