文件结构
Java网络编程_netTest1Client/com.test1/MyClient1.java
/**
*这是一个客户端程序,可以连接服务器端
*/
packagecom.test1;
importjava.io.*;
importjava.net.*;
publicclassMyClient1 {
/**
* @paramargs
*/
publicstaticvoidmain(String[] args) {
// TODOAuto-generated method stub
MyClient1 mc1=newMyClient1();
}
publicMyClient1()
{
try{
//Socket()就是去连接某个服务器端,127.0.0.1表示服务器的IP
//9999就是端口号
Socket s=newSocket("127.0.0.1",9999);
//如果s连接成功,就可以发送数据给服务器
//通过pw,向s写数据,true表示即时刷新
PrintWriter pw=newPrintWriter(s.getOutputStream(),true);
pw.println("你好吗?我是客户端");
//客户端接收
InputStreamReader isr=newInputStreamReader(s.getInputStream());
BufferedReader br=newBufferedReader(isr);
String response=br.readLine();
System.out.println("我是客户端!\n我收到了服务器回复的信息:"+response);
} catch(IOException e) {
e.printStackTrace();
}
}
}
Java网络编程_netTest1Client/com.test1/MyServer1.java
/**
*这是第一个服务器端程序,让它在9999端口监听
*可以接收从客户端发来的信息
*/
packagecom.test1;
importjava.io.*;
importjava.net.*;
publicclassMyServer1 {
publicstaticvoidmain(String []args)
{
MyServer1 ms1=newMyServer1();
}
publicMyServer1()
{
try{
//在9999号端口监听
ServerSocket ss=newServerSocket(9999);
System.out.println("我是服务器,在9999端口监听...");
//等待某个客户端来连接,该函数会返回一个Scoket连接
Socket s=ss.accept();
System.out.println("有客户端连接");
//要读取取s中传递的数据
InputStreamReader isr=newInputStreamReader(s.getInputStream());
BufferedReader br=newBufferedReader(isr);
String info=br.readLine(); //读
System.out.println("服务器接收到:"+info);
//服务器回复一句话
PrintWriter pw=newPrintWriter(s.getOutputStream(),true);//true表示即时刷新
pw.println("我是服务器,客户端你也好!");
} catch(IOException e) {
e.printStackTrace();
}
}
}
Java网络编程_netTest2Client/com.test1/MyClient2.java
/**
*半双工通信
*功能:客户端
*/
packagecom.test1;
importjava.net.*;
importjava.io.*;
publicclassMyClient2 {
/**
* @paramargs
*/
publicstaticvoidmain(String[] args) {
// TODOAuto-generated method stub
MyClient2 mc2=newMyClient2();
}
publicMyClient2()
{
try{
//连接服务器端
Socket s=newSocket("127.0.0.1",9999);
//向s写入数据
PrintWriter pw=newPrintWriter(s.getOutputStream(),true);
//先接收从控制台输入的信息
InputStreamReader isr=newInputStreamReader(System.in);
BufferedReader br=newBufferedReader(isr);
//接收从服务器端发送过来的信息
InputStreamReader isr2=newInputStreamReader(s.getInputStream());
BufferedReader br2=newBufferedReader(isr2);
while(true)
{
System.out.println("请输入你想对服务器说的话:");
//客户端先从控制台接收
String info=br.readLine();
//然后把从控制台输入的信息发送给服务器
pw.println(info);
if(info.equals("bye"))
{
System.out.println("对话结束");
s.close();
break;
}
//接收从服务器发来的话
String res=br2.readLine();
System.out.println("服务器说:"+res);
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
Java网络编程_netTest2Client/com.test1/ MyServer2.java
/**
*半双工通信
*功能:一个服务器端,在9999端口上监听
*/
packagecom.test1;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.PrintWriter;
importjava.net.*;
importjava.io.*;
publicclassMyServer2 {
/**
* @paramargs
*/
publicstaticvoidmain(String[] args) {
// TODOAuto-generated method stub
MyServer2 ms2=newMyServer2();
}
publicMyServer2()
{
try{
//在9999端口监听
ServerSocket ss=newServerSocket(9999);
System.out.println("服务器在9999监听...");
//等待连接
Socket s=ss.accept();
//向s中写信息
PrintWriter pw=newPrintWriter(s.getOutputStream(),true);
//先接收客户端发来的信息(Socket上的)
InputStreamReader isr=newInputStreamReader(s.getInputStream());
BufferedReader br=newBufferedReader(isr);
//接收从控制台输入的信息
InputStreamReader isr2=newInputStreamReader(System.in);
BufferedReader br2=newBufferedReader(isr2);
while(true)
{
String infoFromClient=br.readLine();
System.out.println("客户端发来:"+infoFromClient);
if(infoFromClient.equals("bye"))
{
System.out.println("对话结束!");
s.close();
break;
}
//接收从控制台输入的信息
System.out.println("输入你希望对客户端说的话:");
String response=br2.readLine();
//把从控制台接受的信息送给客户端
pw.println(response);
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
Java网络编程_netTest3Client/com.test1/MyClient3.java
/**
*全双工通信
*客户
*/
packagecom.test1;
importjavax.swing.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.*;
importjava.net.*;
publicclassMyClient3
extendsJFrame implementsActionListener{
JTextArea jta=null;
JTextField jtf=null;
JButton jb=null;
JScrollPane jsp=null;
JPanel jp1=null;
//把信息发给客户端的对象
PrintWriter pw=null;
publicstaticvoidmain(String[] args) {
// TODOAuto-generated method stub
MyClient3 ms=newMyClient3();
}
publicMyClient3()
{
jta=newJTextArea();
jsp=newJScrollPane(jta);
jtf=newJTextField(10);
jb=newJButton("发送");
jb.addActionListener(this);
jp1=newJPanel();
jp1.add(jtf);
jp1.add(jb);
this.add(jsp,"Center");
this.add(jp1,"South");
this.setSize(300,200);
this.setTitle("QQ简易聊天-客户端");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try{
Socket s=newSocket("127.0.0.1",9988);
InputStreamReader isr=newInputStreamReader(s.getInputStream());
BufferedReader br=newBufferedReader(isr);
pw=newPrintWriter(s.getOutputStream(),true);
while(true)
{
//不停的读取从服务器发来的信息
String info=br.readLine();
jta.append("服务器端对客户端说:"+info+"\r\n");
}
} catch(Exception e) {
e.printStackTrace();
}
}
@Override
publicvoidactionPerformed(ActionEvent e) {
//如果用户按下发送信息的按钮
if(e.getSource()==jb)
{
//把服务器在jtf写的内容发送给客户端
String info=jtf.getText();
//把客户端显示的信息加到jta
jta.append("客户端对服务器说:"+info+"\r\n");
pw.println(info);
//清空jtf中的内容
jtf.setText("");
}
}
}
Java网络编程_netTest3Client/com.test1/ MyServer3.java
/**
*全双工通信
*服务器端
*/
packagecom.test1;
importjavax.swing.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.*;
importjava.net.*;
publicclassMyServer3
extendsJFrame implementsActionListener{
JTextArea jta=null;
JTextField jtf=null;
JButton jb=null;
JScrollPane jsp=null;
JPanel jp1=null;
//把信息发给客户端的对象
PrintWriter pw=null;
publicstaticvoidmain(String[] args) {
// TODOAuto-generated method stub
MyServer3 ms=newMyServer3();
}
publicMyServer3()
{
jta=newJTextArea();
jsp=newJScrollPane(jta);
jtf=newJTextField(10);
jb=newJButton("发送");
jb.addActionListener(this);
jp1=newJPanel();
jp1.add(jtf);
jp1.add(jb);
this.add(jsp,"Center");
this.add(jp1,"South");
this.setSize(300,200);
this.setTitle("QQ简易聊天-服务器端");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try{
//服务器监听
ServerSocket ss=newServerSocket(9988);
//等待客户端连接
Socket s=ss.accept();
//System.out.println("服务器端在9988监听!");
InputStreamReader isr=newInputStreamReader(s.getInputStream());
BufferedReader br=newBufferedReader(isr);
pw=newPrintWriter(s.getOutputStream(),true);
//读取从客户端发来的信息
while(true)
{
//读取从客户端发来的信息
String info=br.readLine();
//把信息显示加到文本域中(以追加的形式)
jta.append("客户端对服务器说:"+info+"\r\n");
}
} catch(IOException e) {
e.printStackTrace();
}
}
@Override
publicvoidactionPerformed(ActionEvent e) {
//如果用户按下发送信息的按钮
if(e.getSource()==jb)
{
//把服务器在jtf写的内容发送给客户端
String info=jtf.getText();
jta.append("服务器端对客户端说:"+info+"\r\n");
pw.println(info);
//清空jtf中的内容
jtf.setText("");
}
}
}
Java网络编程_netTestClient/com.test1/MyServer.java
/**
*功能:网络这间利用对象流
*/
packagecom.test1;
importjava.net.*;
importjava.io.*;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importcom.common.*;
publicclassMyServer {
/**
* @paramargs
*/
publicstaticvoidmain(String[] args) {
// TODOAuto-generated method stub
MyServer ms=newMyServer();
}
publicMyServer()
{
try{
ServerSocket ss=newServerSocket(3456);
System.out.println("服务器在3456端口监听...");
Socket s=ss.accept();
//以对象流方式读取(假设客户端发送的是User的一个对象)
ObjectInputStream ois=newObjectInputStream(s.getInputStream());
User u=(User)ois.readObject();//读取对象
//输出
System.out.println("从客户端接收到:"+u.getName()+" "+u.getPass());
s.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Java网络编程_netTestClient/com.common/User.java
packagecom.common;
publicclassUser
implementsjava.io.Serializable{
//将这个类实现序列化
privateString name;
privateString pass;
publicString getName() {
returnname;
}
publicvoidsetName(String name) {
this.name= name;
}
publicString getPass() {
returnpass;
}
publicvoidsetPass(String pass) {
this.pass= pass;
}
}
Java网络编程_netTestClient/com.test1/MyClient.java
packagecom.test1;
importjava.io.*;
importjava.net.*;
importcom.common.User;
importcom.test1.*;
publicclassMyClient {
/**
* @paramargs
*/
publicstaticvoidmain(String[] args) {
//
MyClient mc=newMyClient();
}
publicMyClient()
{
try{
Socket s=newSocket("127.0.0.1",3456);
//通过ObjectOutputStream给服务器传送对象
ObjectOutputStream oos=newObjectOutputStream(s.getOutputStream());
User u=newUser();
u.setName("维唯为为");
u.setPass("123456");
oos.writeObject(u); //写入对象流
//如果希望一个对象在网络上传输,则需要把这个对象序列化
//System.out.println("ok");
s.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
Java网络编程_netTestClient/com.common/User.java
packagecom.common;
publicclassUser
implementsjava.io.Serializable{
//将这个类实现序列化
privateString name;
privateString pass;
publicString getName() {
returnname;
}
publicvoidsetName(String name) {
this.name= name;
}
publicString getPass() {
returnpass;
}
publicvoidsetPass(String pass) {
this.pass= pass;
}
}