我要完成这样一个需求,客户端像服务器端发送数据,服务器接收到数据后,在返回一个数据给客户端
。
下面是我写的代码:现在已经完成了客户端向服务器端发送数据,那么服务器端怎么像客户端发送数据呢
/**
* 客户端
*/
package Client;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.UnknownHostException;
import java.util.Scanner;
import SSocket.SSocket;import ChooseMpl.ChooseMpl;public class ClientTest { public static void main(String[] args) throws IOException {
Socket sk=null;
//接收用户的输入
System.out.println("欢迎使用在线迷你TXT小时管理器");
System.out.println("1.登录\n2.注册\n3.退出\n请选择:");
Scanner input = new Scanner(System.in);
int a=input.nextInt();//接收用户输入
/*
* 实现向服务器传送数据的操作
*/
InetAddress add=InetAddress.getLocalHost();
String ip=add.getHostAddress();//获得服务器IP,此处也就是本机ip
System.out.println(ip);//获得服务器IP,此处也就是本机ip
sk=new Socket(ip,8088);//向服务器的ip的8088端口发起连接请求
OutputStream out=sk.getOutputStream();//返回此套节字的输出流
PrintWriter os=new PrintWriter(out);//os对象是为了向服务器传入数据
os.print(a);
os.flush();//刷新
/**
* 接收服务器返回的数据
*/
PrintWriter oss=new PrintWriter(out);
BufferedReader is=new BufferedReader(new InputStreamReader(sk.getInputStream()));
//服务器返回数据
String res=is.readLine();
System.out.println(res);
String name=input.next();
System.out.println(name);
os.flush();//刷新
oss.flush(); //释放资源
os.close();
is.close();
oss.close();
}
}
/**
* 线程类
*/
package SSocket;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;public class SSocket extends Thread {
Socket sk=null;
public SSocket(Socket socket){
this.sk=socket;
} public void run(){//定义线程的run方法
System.out.println("线程开始运行,此线程是会交替运行的");
//BufferedReader为输入流,用来接收客户端输入,或者服务器端输入
BufferedReader input;
//PrintWriter为输出流,用来发送客户端输出,或者服务器端输出
PrintWriter output;
try {//实现数据的接收和发送
input=new BufferedReader(new InputStreamReader(sk.getInputStream()));
output=new PrintWriter(sk.getOutputStream());
String msg=input.readLine();
//System.out.println("服务器接受客户端数据为:"+msg);
output.flush();//刷新数据
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
* 服务器端
package Server;import java.io.BufferedReader;public class ServerTest extends HttpServlet {//需要继承自HttpServlet类
public static void main(String[] args) {
System.out.println("Socket服务器开始启动!");
ServerSocket ssk=null;
try {
//绑定端口,和客户端一致
ssk=new ServerSocket(8088);
while(true){//此时用while(true)的作用是为了让循环一直继续执行下去,也就是说只要客户端有输入,服务器都会不停的去接收客户端的数据
//调用方法接受客户端的请求,并返回Socket套节字,用client获取客户端所发送到信息
Socket client=ssk.accept();
//对client分析从中得到客户端的请求数据,BufferedReader用来从字符输入流中读取文本,客户端请求数据通过这行代码就被缓冲到了input对象中
BufferedReader input=new BufferedReader(new InputStreamReader(client.getInputStream()));
String msg=input.readLine();//得到input对象的值
System.out.println("服务器接收到的内容为:"+msg);
//添加线程SSocket
SSocket st=new SSocket(client);//将客户端的请求放入线程,需要创建SSocket线程
new Thread(st).start();//执行线程
String mes="冯硕";
//定义输出流,向客户端发送服务器返回的信息
byte[] buf = mes.getBytes();
// 确定发送方IP地址及端口号,地址为本地主机地址
InetAddress add=InetAddress.getLocalHost();
int port = 8087;
InetAddress ip = InetAddress.getByName(add.getHostAddress());//获得服务器IP,此处也就是本机ip
System.out.println("开始向客户端发送数据");
// 创建发送类型的数据报
DatagramPacket sendPacket = new DatagramPacket(buf, buf.length, ip,port);
// 通过套接字发送数据
DatagramSocket sendSocket = new DatagramSocket();
sendSocket.send(sendPacket);
new Thread(st).start();//执行线程
//将服务器数据返回给客户端,参见:F:\北大青鸟\资料\NetProTest\src\com\Demo4
//通过数据报得到发送方的IP地址、端口和套接字地址
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(ssk!=null)//当不等于空,也就是说是一个对象的时候,执行下面try语句中的关闭操作
try{
ssk.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
。
下面是我写的代码:现在已经完成了客户端向服务器端发送数据,那么服务器端怎么像客户端发送数据呢
/**
* 客户端
*/
package Client;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.UnknownHostException;
import java.util.Scanner;
import SSocket.SSocket;import ChooseMpl.ChooseMpl;public class ClientTest { public static void main(String[] args) throws IOException {
Socket sk=null;
//接收用户的输入
System.out.println("欢迎使用在线迷你TXT小时管理器");
System.out.println("1.登录\n2.注册\n3.退出\n请选择:");
Scanner input = new Scanner(System.in);
int a=input.nextInt();//接收用户输入
/*
* 实现向服务器传送数据的操作
*/
InetAddress add=InetAddress.getLocalHost();
String ip=add.getHostAddress();//获得服务器IP,此处也就是本机ip
System.out.println(ip);//获得服务器IP,此处也就是本机ip
sk=new Socket(ip,8088);//向服务器的ip的8088端口发起连接请求
OutputStream out=sk.getOutputStream();//返回此套节字的输出流
PrintWriter os=new PrintWriter(out);//os对象是为了向服务器传入数据
os.print(a);
os.flush();//刷新
/**
* 接收服务器返回的数据
*/
PrintWriter oss=new PrintWriter(out);
BufferedReader is=new BufferedReader(new InputStreamReader(sk.getInputStream()));
//服务器返回数据
String res=is.readLine();
System.out.println(res);
String name=input.next();
System.out.println(name);
os.flush();//刷新
oss.flush(); //释放资源
os.close();
is.close();
oss.close();
}
}
/**
* 线程类
*/
package SSocket;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;public class SSocket extends Thread {
Socket sk=null;
public SSocket(Socket socket){
this.sk=socket;
} public void run(){//定义线程的run方法
System.out.println("线程开始运行,此线程是会交替运行的");
//BufferedReader为输入流,用来接收客户端输入,或者服务器端输入
BufferedReader input;
//PrintWriter为输出流,用来发送客户端输出,或者服务器端输出
PrintWriter output;
try {//实现数据的接收和发送
input=new BufferedReader(new InputStreamReader(sk.getInputStream()));
output=new PrintWriter(sk.getOutputStream());
String msg=input.readLine();
//System.out.println("服务器接受客户端数据为:"+msg);
output.flush();//刷新数据
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
* 服务器端
package Server;import java.io.BufferedReader;public class ServerTest extends HttpServlet {//需要继承自HttpServlet类
public static void main(String[] args) {
System.out.println("Socket服务器开始启动!");
ServerSocket ssk=null;
try {
//绑定端口,和客户端一致
ssk=new ServerSocket(8088);
while(true){//此时用while(true)的作用是为了让循环一直继续执行下去,也就是说只要客户端有输入,服务器都会不停的去接收客户端的数据
//调用方法接受客户端的请求,并返回Socket套节字,用client获取客户端所发送到信息
Socket client=ssk.accept();
//对client分析从中得到客户端的请求数据,BufferedReader用来从字符输入流中读取文本,客户端请求数据通过这行代码就被缓冲到了input对象中
BufferedReader input=new BufferedReader(new InputStreamReader(client.getInputStream()));
String msg=input.readLine();//得到input对象的值
System.out.println("服务器接收到的内容为:"+msg);
//添加线程SSocket
SSocket st=new SSocket(client);//将客户端的请求放入线程,需要创建SSocket线程
new Thread(st).start();//执行线程
String mes="冯硕";
//定义输出流,向客户端发送服务器返回的信息
byte[] buf = mes.getBytes();
// 确定发送方IP地址及端口号,地址为本地主机地址
InetAddress add=InetAddress.getLocalHost();
int port = 8087;
InetAddress ip = InetAddress.getByName(add.getHostAddress());//获得服务器IP,此处也就是本机ip
System.out.println("开始向客户端发送数据");
// 创建发送类型的数据报
DatagramPacket sendPacket = new DatagramPacket(buf, buf.length, ip,port);
// 通过套接字发送数据
DatagramSocket sendSocket = new DatagramSocket();
sendSocket.send(sendPacket);
new Thread(st).start();//执行线程
//将服务器数据返回给客户端,参见:F:\北大青鸟\资料\NetProTest\src\com\Demo4
//通过数据报得到发送方的IP地址、端口和套接字地址
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(ssk!=null)//当不等于空,也就是说是一个对象的时候,执行下面try语句中的关闭操作
try{
ssk.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
import java.net.* ;
import java.io.* ;
public class EchoClient{
public static void main(String args[]) throws Exception { // 所有异常抛出
Socket client = null ; // 表示客 户端
client = new Socket("localhost",8888) ;
BufferedReader buf = null ; // 一次性接收完成
PrintStream out = null ; // 发送数据
BufferedReader input = null ; // 接收键盘数据
input = new BufferedReader(new InputStreamReader(System.in)) ;
buf = new BufferedReader(new InputStreamReader(client.getInputStream())) ;
out = new PrintStream(client.getOutputStream()) ;
boolean flag = true ; // 定义标志位
while(flag){
System.out.print("输入信息:") ;
String str = input.readLine() ; // 接收键盘的输入信息
out.println(str) ;
if("bye".equals(str)){
flag = false ;
}else{
String echo = buf.readLine() ; // 接收返回结果
System.out.println(echo) ; // 输出回应信息
}
}
buf.close() ;
client.close() ;
}
};
import java.net.* ;
import java.io.* ;
public class EchoServer{
public static void main(String args[]) throws Exception { // 所有异常抛出
ServerSocket server = null ; // 定义ServerSocket类
Socket client = null ; // 表示客 户端
BufferedReader buf = null ; // 接收输入流
PrintStream out = null ; // 打印流输出最方便
server = new ServerSocket(8888) ; // 服务器在8888端口上监听
boolean f = true ; // 定义个标记位
while(f){
System.out.println("服务器运行,等待客户端连接。") ;
client = server.accept() ; // 得到连接,程序进入到阻塞状态
out = new PrintStream(client.getOutputStream()) ;
// 准备接收客户端的输入信息
buf = new BufferedReader(new InputStreamReader(client.getInputStream())) ;
boolean flag = true ; // 标志位,表示可以一直接收并回应信息
while(flag){
String str = buf.readLine() ; // 接收客户端发送的内容
if(str==null||"".equals(str)){ // 表示没有内容
flag = false ; // 退出循环
}else{
if("bye".equals(str)){ // 如果输入的内容为bye表示结束
flag = false ;
}else{
out.println("ECHO : " + str) ; // 回应信息
}
}
}
client.close() ;
}
server.close() ;
}
};