import java.io.*;
public class ServerClass{
public static void main(String args[]){
int i=0;
try{
ServerSocket ss=new ServerSocket(2345);//为该服务器开设一个端口,该端口供客户机程序来访问。
System.out.println("服务器启动......");
while(true){
Socket sock=ss.accept();//sock对象接收客户机的连接请求,程序运行到这里将处于等待状态。
i++;
System.out.println("接受连接请求"+i);
PrintStream out=new PrintStream(sock.getOutputStream());//获得输出流
out.println("你好");
out.close();
sock.close();
}
}catch(IOException e){
System.out.println(e);
}
}
}
import java.io.*;
import java.net.*;
public class AClient{
public static void main(String args[]){
try{
Socket sock=new Socket("127.0.0.1",2345);//根据具体的(正在用的机器的)IP和端口,创建一个Socket对象
BufferedReader in=new BufferedReader(new InputStreamReader(sock.getInputStream()));//获得输入流。
String s=in.readLine();//从输入流读入一行内容。
System.out.println(s);//打印出
in.close();
}catch(IOException e){}
}
}
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
用户界面ServerUI
*/
public class ServerUI extends JFrame{
JTextArea mainArea;
JTextArea sendArea;
JTextField indexArea;
SvrCom server;
public void setServer( SvrCom server )
{ this.server = server; }
public ServerUI()
{
super("服务器端");
Container contain = getContentPane();
contain.setLayout( new BorderLayout() );
mainArea = new JTextArea();
JScrollPane mainAreaP = new JScrollPane( mainArea );
JPanel panel = new JPanel();
panel.setLayout( new BorderLayout() );
sendArea = new JTextArea(3, 8);
JButton sendBtn = new JButton("发送");
sendBtn.addActionListener( new ActionListener()//注册动作监听器
{
public void actionPerformed(ActionEvent ae)
{
server.sendMsg(sendArea.getText());//把信息传递到客户端
mainArea.append( "【服务器】" + sendArea.getText() + "\n" );//把信息显示在服务器的聊天记录区域
sendArea.setText("");
}
}
);
JPanel tmpPanel = new JPanel();
indexArea = new JTextField(2);
indexArea.setText("0");
tmpPanel.add( sendBtn );
tmpPanel.add( indexArea );
panel.add( tmpPanel, BorderLayout.EAST );
panel.add(sendArea, BorderLayout.CENTER );
contain.add( mainAreaP, BorderLayout.CENTER );
contain.add( panel, BorderLayout.SOUTH );
setSize( 500, 300);
setVisible( true );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
ServerUI ui = new ServerUI();
SvrCom server = new SvrCom( ui );//创建并启动网络通讯线程,准备接受客户端数据包
}
}
/**
通讯类SvrCom负责守候数据到来
*/
class SvrCom extends Thread//网络通讯类
{
Socket client;
ServerSocket soc;
BufferedReader in;
PrintWriter out;
ServerUI ui;
// ChatServer server;
public SvrCom( ServerUI ui ){ //初始化SvrCom类
this.ui = ui;
ui.setServer( this );
try {
soc = new ServerSocket(6666 ); //开设服务器端口6666
System.out.println( "启动服务器成功,等待端口号:6666" );
client = soc.accept();//当客户机请求连接时,创建一条链接
System.out.println( "连接成功!来自" + client.toString() );
in = new BufferedReader( new InputStreamReader( client.getInputStream()) );
out = new PrintWriter( client.getOutputStream() , true);
} catch( Exception ex ) { System.out.println( ex ); }
start();
}
public void run () {// 用于监听客户端发送来的信息
String msg = "";
while( true )
{
try {
msg = in.readLine();//从in对象上读数据信息
}
catch( SocketException ex ) {
System.out.println(ex);
break;
}
catch( Exception ex ) { System.out.println(ex); }
if( msg!=null && msg.trim()!="" )
{
System.out.println(">>" + msg );
ui.mainArea.append( msg + "\n" );
}
}
}
public void sendMsg( String msg ) {// 用于发送信息
try {
out.println("【服务器】" + msg );//把信息写入输出流
}
catch(Exception e) {
System.out.println( e );
}
}
}
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
用户界面ClientUI
*/
public class ClientUI extends JFrame
{
JTextArea mainArea;
JTextArea sendArea;
ChatClient client;
JTextField ipArea;
JButton btnLink;
public void setClient( ChatClient client )
{ this.client = client; }
public ClientUI()
{
super("客户端");
Container contain = getContentPane();
contain.setLayout( new BorderLayout() );
mainArea = new JTextArea();
JScrollPane mainAreaP = new JScrollPane( mainArea );//为文本区添加滚动条
JPanel panel = new JPanel();
panel.setLayout( new BorderLayout());
sendArea = new JTextArea(3, 8);
JButton sendBtn = new JButton("发送");
sendBtn.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
client.sendMsg( sendArea.getText() );
mainArea.append( "【客户端】"+sendArea.getText() + "\n" );
sendArea.setText("");
}
}
);
JPanel ipPanel = new JPanel();
ipPanel.setLayout( new FlowLayout( FlowLayout.LEFT , 10, 10 ) );
ipPanel.add( new JLabel("服务器:") );
ipArea = new JTextField( 12 ) ;
ipArea.setText( "127.0.0.1" );
ipPanel.add( ipArea );
btnLink = new JButton("连接");
ipPanel.add( btnLink );
btnLink.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
client = new ChatClient( ipArea.getText() , 6666, ClientUI.this );//创建socket对象
ClientUI.this.setClient( client );
}
}
);
panel.add( sendBtn, BorderLayout.EAST );
panel.add( sendArea, BorderLayout.CENTER );
contain.add( ipPanel, BorderLayout.NORTH );
contain.add( mainAreaP, BorderLayout.CENTER );
contain.add( panel, BorderLayout.SOUTH );
setSize( 500, 300);
setVisible( true );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args)
{
ClientUI ui = new ClientUI();
}
}
/**
通讯类ChatClient负责守候数据到来
*/
class ChatClient extends Thread
{
Socket sc;// 对象sc,用来处理与服务?鞯耐ㄐ?
BufferedReader in;//声明输入流缓冲区,用于存储服务器发来的信息
PrintWriter out;//声明打印输出流,用于信息的发送
ClientUI ui;
public ChatClient( String ip, int port, ClientUI ui ){//初始化ChatClient类
this.ui = ui;
try{
sc = new Socket(ip , port); // 创建sc, 用服务器ip和端口作参数
System.out.println("已顺利联接到服务器。");
out = new PrintWriter( sc.getOutputStream(), true );
in = new BufferedReader( new InputStreamReader( sc.getInputStream() ) );
}
catch(Exception e) {
System.out.println( e );
}
start();
}
public void run () { // 用于监听服务器端发送来的信息
String msg = "";
while( true )
{
try {
msg = in.readLine();//从缓冲区读入一行字符存于msg
} catch( SocketException ex ) { System.out.println(ex); break; }
catch( Exception ex ) { System.out.println(ex); }
if( msg!=null && msg.trim()!="" )
{//若msg信息不为空
System.out.println(">>" + msg );
ui.mainArea.append( msg + "\n" );//把msg信息添加到客户端的文本区域内
}
}
}
public void sendMsg( String msg ) {// 用于发送信息
try {
out.println("【客户端】" + msg );
}
catch(Exception e) {
System.out.println( e );
}
}
}