java 假死_JAVA TCP通信基础篇——对发消息【如何解决输入流假死状态】

  • Post author:
  • Post category:java


TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议.

面向连接:服务器创建一个连接端口,客户端去连接服务器实现通信。

可靠的:tcp协议是是在连接成功之后才会发送数据,而基于UDP的协议是事先不会征求对方意见,直接将数据传送出去,因此TCP协议传输中,数据更可靠,更安全。

基于字节流:tcp协议中传递的信息是以流的形式传递的,在java中可通过inputstream来捕获,通过outputstream发送。

对于接收数据的时候出现假死的状态:

readLine()函数读取一行,结尾需要”\n”才能结束,不然会一直卡死在这里,因此大家在用printwriter的时候要print一个”\n”,然后把printwriter flush掉。就不会假死了!

这样:pw.write(“fuck you cient”);    //send to server

pw.write(“\n”);

pw.flush();

在java中,tcp协议是已经封装好的类,可直接供用户使用。其中关键的两个类是

Socket类     ServerSocket类。

ServerSocket类用来创建服务器,具体用法请参照jdk文档。常用用法是

ServerSocket ss = new ServerSocket((int)port) //传入一个端口号,用户自定

Socket用于控制两端的连接,服务器的Socket可以和客户端的Socket相连接:

f847770e65f321fec77938968dfddd4f.png

socket可以使用inputstream和outputstream进行相互通信。

(服务器中通常是将已将建立连接的socket丢给子线程通信,但是点对点可以让服务器处理一个socket就行了,不需要再生成子线程进行通信交流)

代码分为三个文件。

cd70761c0c0003012bbbb1c02c814cb6.png

程序运行结果:

f828fb166172a416ab6117299e620ec2.png

以下是代码附上:

//package org.cat.Client;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.net.Socket;

public class MyClient {

public static void main(String[] args){

try {

Socket s = new Socket(“localhost”, 12341);

System.out.println(“conneting to server successfully”);

//successfully connecting to server

//Now sending some data to server

//1.create outputStream and so on…

OutputStream out = s.getOutputStream();

PrintWriter pw = new PrintWriter(out);

pw.write(“fuck you server”);//send to server

pw.write(“\n”);//important

pw.flush();

System.out.println(“I have sended to server”);

//2.receiving data from server

InputStream in = s.getInputStream();

InputStreamReader inReader = new InputStreamReader(in);

BufferedReader bfdReader = new BufferedReader(inReader);

String tmp = “xxxx,receiving failed”;

while((tmp = bfdReader.readLine()) != null)

System.out.println(“server tells:”+tmp);

//close all

in.close();inReader.close();bfdReader.close();

out.close();pw.close();

} catch (IOException e) {

System.out.println(“connecting failed,have u opened server?”);

e.printStackTrace();

}

}

}

//package org.cat.Server;

import java.io.IOException;

import java.net.ServerSocket;

import java.net.Socket;

public class MyServer {

public static void main(String[] args){

try {

@SuppressWarnings(“resource”)

ServerSocket sers = new ServerSocket(12341);

//then wait for client to connect

Socket s;

System.out.println(“waiting for client to connect”);

s = sers.accept();

new MyThread(s);

} catch (IOException e) {

System.out.println(“set up server failed??”);

e.printStackTrace();

}

}

}

//package org.cat.Server;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.net.Socket;

public //service for every client

class MyThread extends Thread{

Socket s;

public MyThread(Socket s) {

this.s = s;

start();

//telling that connection has been settle up

System.out.println(“successed to connet with port of”+s.getLocalPort());

//now receiving data and talk back to client

//1.receive

try {

InputStream in = s.getInputStream();

InputStreamReader inReader = new InputStreamReader(in);

BufferedReader bfdReader = new BufferedReader(inReader);

String tmp = “xxxx,receiving failed”;

if((tmp = bfdReader.readLine()) != null)

System.out.println(“client tells:”+tmp);

//anwser client

OutputStream out = s.getOutputStream();

PrintWriter pw = new PrintWriter(out);

pw.write(“fuck you cient”);//send to server

pw.write(“\n”);

pw.flush();

System.out.println(“I have sended to client”);

//close all

out.close();pw.close();

in.close();inReader.close();bfdReader.close();

} catch (IOException e1) {

System.out.println(“get inputStream from client failed”);

e1.printStackTrace();

}

System.out.println(“now to shut connection”);

try {

s.close();

System.out.println(“shut down connection successfully”);

} catch (IOException e) {

System.out.println(“shut server failed,have u opened it?”);

e.printStackTrace();

}

}

}



版权声明:本文为weixin_28858229原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。