标签:accept 握手 soc ref nal toc server 文章 数据传输
//<--------------服务端代码-------------------->
public class SocketReadLister implements Runnable {
private final int tcpPort=9999;
private ServerSocket serverSocket;
@Override
public void run() {
try {
serverSocket = new ServerSocket(this.tcpPort);
while(true){
Socket socket = serverSocket.accept();
//socket.setSoTimeout(5*1000);//设置读取数据超时时间为5s
new Thread(new SocketReadThread(socket)).start();
}
}catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception{
new Thread(new SocketReadLister()).start();
}
}
public class SocketReadThread implements Runnable {
private Socket socket;
public SocketReadThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
byte[] data = new byte[1024];
try {
InputStream is=socket.getInputStream();
int length=0;
int num=is.available();
while((length = is.read(data)) != -1){
String result = new String(data);
System.out.println("数据available:"+num);
System.out.println("数据:"+result);
System.out.println("length:" + length);
}
System.out.print("结束数据读取:"+length);
}catch (SocketTimeoutException socketTimeoutException){
try {
Thread.sleep(2*1000);
}catch (Exception e) {
e.printStackTrace();
}
run();
} catch (Exception e){
e.printStackTrace();
try {
socket.close();
}catch (IOException io){
io.printStackTrace();
}
}
}
}
//<---------------------客户端代码---------------------------->
public class SocketClient implements Runnable {
private final int tcpPort=9999;
private Socket socket;
@Override
public void run() {
String msg = "ab23567787hdhfhhfy";
byte[] byteMsg = msg.getBytes();
try {
socket = new Socket("127.0.0.1", 9999);
OutputStream out = socket.getOutputStream();
InputStream inputStream=socket.getInputStream();
out.write(byteMsg);
Thread.sleep(10*1000);
char[] chars=msg.toCharArray();
String str="";
/*out.flush();*/
for(int i=0;i<msg.length();i++) {
str=chars[i]+"-"+i;
out.write(str.getBytes());
Thread.sleep(1*1000);
}
byte[] bytes=new byte[8];
while(true) {
if(inputStream.available()>0) {
if(inputStream.read(bytes)!=-1) {
System.out.println(new String(bytes));
}
}
Thread.sleep(10*1000);
}
} catch (Exception e) {
e.printStackTrace();
try {
socket.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
public static void main(String[] args) {
new Thread(new SocketClient()).start();
}
}
while(true){
if(is.available()>0){
is.read(data);
}
}
if (nRecv < nRecvNeed){
int nSize = 0;
wsaBuf=new byte[nRecvNeed-nRecv];
int readCount = 0; // 已经成功读取的字节的个数
try {
while (readCount < wsaBuf.length) {
//Thread.sleep(100);//读取之前先将线程休眠,避免循环时,程序占用CPU过高
try {
availableNum=inputStream.available();
if(availableNum>0){
readCount += inputStream.read(wsaBuf, readCount, (wsaBuf.length - readCount));//避免数据读取不完整
}
}catch (SocketTimeoutException timeOut){
System.out.println("读取超时,线程执行休眠操作,2秒后再读取");
Thread.sleep(2*1000);
}
}
}catch (Exception e){
System.out.println("读取数据异常");
e.printStackTrace();
close();//关闭socket连接
break;
}
nSize=wsaBuf.length;
nRecv+=nSize;
}
标签:accept 握手 soc ref nal toc server 文章 数据传输
原文地址:https://www.cnblogs.com/onedayinMay/p/12203599.html