客户端输入一个数,服务端接收后按照自定义协议返回数据
客户端收到服务端返回的数据,根据自定义协议解析,输出结果
测试时需先启动服务端/微笑
1、客户端
package com.linoer.http;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
/**
* @author linoer
*
*/
public class TCPJudgeDemoClient {
private static BufferedReader br;
private static Socket socket;
private static InputStream in;
private static OutputStream out;
private final static String HOST = "127.0.0.1";
private final static int PORT = 10006;
public static void main(String[] args) {
init();
while(true){
System.out.println("请输入一个要判断质数的数字:");
String input = readInput();
if(isQiut(input)){
byte[] bytes = "quit".getBytes();
send(bytes);
break;
}
if(checkInput(input)){
System.out.println("合格数据为:"+input);
System.out.println("转换协议后的格式为:"+buildInput(input));
//input = buildInput(input);
//发送数据
send(input.getBytes());
//接收数据
byte[] result = receive();
parseData(result);
}else{
System.out.println("不合法输入");
}
}
close();
}
/**
*
*/
private static void close() {
// TODO Auto-generated method stub
try {
br.close();
in.close();
out.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param input
* @return
*/
private static String buildInput(String input) {
return input+".";
}
/**
* 解析协议
* @param res
*/
public static void parseData(byte[] res){
if(res ==null){
System.out.println("服务端返回数据出错");
return ;
}
byte value = res[0];
//按照协议解析,0质数,1不是质数,2协议格式错误
switch (value) {
case 0:
System.out.println("质数");
break;
case 1:
System.out.println("不是质数");
break;
case 2:
System.out.println("协议格式出错");
break;
default:
break;
}
}
/**
* 接收数据
* @return
*/
public static byte[] receive(){
byte[] b = new byte[1024];
int n;
try {
n = in.read(b);
byte[] cpdata = new byte[n];
System.arraycopy(b, 0, cpdata, 0, n);
return cpdata;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* 判断用户输入是否合法
* @param s
* @return
*/
public static Boolean checkInput(String s){
if(s == null){
return false;
}else{
int n = Integer.parseInt(s);
if(n>=2){
return true;
}else{
return false;
}
}
}
/**
* 发送数据给客户端
* @param b
*/
public static void send(byte[] b){
try {
out.write(b);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 判断是否结束
* @param s
* @return
*/
public static Boolean isQiut(String s){
if(s == null){
return false;
}else{
if("quit".equalsIgnoreCase(s)){
return true;
}else{
return false;
}
}
}
/**
* 初始化
*/
public static void init(){
//获取键盘输入
br = new BufferedReader(new InputStreamReader(System.in));
//实例化socket,io流
try {
socket = new Socket(HOST, PORT);
in = socket.getInputStream();
out = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取输入
* @return 返回用户输入的数
*/
public static String readInput(){
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
2、服务端
package com.linoer.http;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author linoer
*
*/
public class TCPJudgeDemoServer {
private static final int PORT = 10006;
private static ServerSocket ss;
public static void main(String[] args) {
try {
ss = new ServerSocket(PORT);
System.out.println("启动服务器");
while(true){
Socket s = ss.accept();
new DemoJudegeThread(s);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
DemoJudgeThread线程类
package com.linoer.http;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
/**
* @author linoer
*
*/
public class DemoJudegeThread extends Thread{
Socket s;
InputStream in;
OutputStream out;
public DemoJudegeThread(Socket s){
this.s = s;
init();
start();
}
/**
*
*/
private void init() {
try {
out = s.getOutputStream();
in = s.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
*/
public void run() {
// TODO Auto-generated method stub
while(true){
//接收客户端的反馈
byte[] data = receive();
//判断是否退出
if(isQuit(data)){
break;
}
byte[] b = judege(data);
send(b);
}
close();
}
/**
*
*/
private void close() {
try {
in.close();
out.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param b
*/
private void send(byte[] b) {
try {
out.write(b);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param data
* @return
*/
private byte[] judege(byte[] data) {
byte[] b = new byte[1];
if(data==null){
b[0] = 2;
return b;
}
Integer i = Integer.parseInt(new String(data));
for (int j = 2; j <= Math.sqrt(i); j++) {
if(i%j==0){
b[0] = 1;
break;
}else{
b[0] = 0;
}
}
return b;
}
/**
* @param data
* @return
*/
private boolean isQuit(byte[] data) {
if(data==null){
return false;
}else{
String s = new String(data);
if(s.equalsIgnoreCase("quit")){
return true;
}else{
return false;
}
}
}
/**
* @return
*/
private byte[] receive() {
byte[] b = new byte[1024];
try {
int n = in.read(b);
byte[] d = new byte[n];
System.arraycopy(b, 0, d, 0, n);
return d;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}