java socket回显服务

根据java tcp/ip编程学习记录 java socket实现回显服务
逻辑比较简单直接代码记录
1.客户端

package com.pjf.echodemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class EchoClient {

public static void main(String[] args) {
Socket socket = null;
InputStream sockIn = null;// 读取
OutputStream sockOut = null;// 输出流

try {
socket = new Socket("localhost", 6666);
/**
* 获取网络输入输出流的实例
*/
sockIn = socket.getInputStream();
sockOut = socket.getOutputStream();

// 从控制台读取
Scanner console = new Scanner(System.in);
String context = null;// 控制台去读内容
// byte[] readBytes = new byte[64];
int length=0;
while (true) {
context = console.nextLine();
// 限制输入内容大小
// if(context.getBytes().length>64){
// System.out.println("输入数据过长无法发送,请缩短内容");
// continue;
// }
length=context.getBytes().length;
sockOut.write(context.getBytes());
sockOut.flush();

//sockIn.read(readBytes, 0, context.getBytes().length);
int rec_len=0;//收到返回消息的长度
int rec_total_len=0;//收到返回消息的总长度
byte[] rev_data=new byte[length];
//读取判断,防止服务端拆分
while(rec_total_len<length){
rec_len=sockIn.read(rev_data,rec_total_len,length-rec_total_len);
if(rec_len==-1){
break;
}
rec_total_len+=rec_len;

}
System.out.println("客户端:"+new String(rev_data, 0, length));
if (context == null || context == "" || context.equalsIgnoreCase("exit")) {
break;
}

}
System.out.println("结束了,goodbye!!!");

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (socket != null) {
if (!socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
socket = null;

}

if (sockIn != null) {

socket = null;

}

if (sockOut != null) {

sockOut = null;

}

}

}

}


2.服务端端:

package com.pjf.echodemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class EchoServer {

public static void main(String[] args) {
ServerSocket ss = null;
Socket socket = null;
InputStream inStream = null;
OutputStream outStream = null;

try {
ss = new ServerSocket(6666);
System.out.println("服务端启动成功=================================");
while (true) {
socket = ss.accept();
System.out.println(
"客户端接收成功" + socket.getLocalAddress().getHostName() + "=================================");
byte[] revdata = new byte[16];
inStream = socket.getInputStream();
outStream = socket.getOutputStream();

int len = 0;
//没有关闭连接则一直读取
while ((len = inStream.read(revdata)) != -1) {

System.out.println("服务端收到:" + new String(revdata, 0, len));
outStream.write(revdata, 0, len);

}

if (outStream != null) {
outStream = null;
}

if (inStream != null) {
inStream = null;
}

if (socket != null) {
if (!socket.isClosed()) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
if (ss != null) {
if (!ss.isClosed()) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}
}



3.由于客户端知道发送的长度,可以等待服务端返回这些内容,进行相应的打印,服务端不知道长度,在服务打印可能都是分开,这个后面可能通过定长编码或者分隔符进行解决,或者每次发送限度不长度


4.存在以下问题:只能够单个socket访问,读写相互堵塞...


5.上面的问题根据后面的章节可以补充....
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值