socket双向通信,为什么服务器只能接收一次客户端发送的消息

socket双向通信,为什么服务器只能接收一次客户端发送的消息,第1张

Socket有两种主要的操作方式:面向连接的和无连接的。(TCP/UDP)  面向连接的Socket操作就像一部电话,Socket必须在发送数据之前与目的地的Socket取得连接,一旦连接建立了,Socket就可以使用一个流接口进行打开、读写以及关闭操作。并且,所有发送的数据在另一端都会以相同的顺序被接收。

注意是相同的顺序

SocketClient1java内容如下:

package compolyphyllasocket;

import javaioBufferedReader;

import javaioIOException;

import javaioInputStreamReader;

import javaioPrintWriter;

import javanetSocket;

import javanetUnknownHostException;

public class SocketClient1 {

private Socket socket;

public SocketClient1() {

try {

socket = new Socket("127001", 2222);

} catch (UnknownHostException e) {

eprintStackTrace();

} catch (IOException e) {

eprintStackTrace();

}

}

/

@param args

/

public static void main(String[] args) {

new SocketClient1()startClient();

}

public void startClient() {

new Thread(new ClientReader(socket))start();

new Thread(new ClientWriter(socket))start();

}

}

class ClientReader implements Runnable {

private Socket socket;

public ClientReader(Socket socket) {

thissocket = socket;

}

public void run() {

BufferedReader br = getReader();

String msg = null;

try {

while ((msg = brreadLine()) != null) {

Systemoutprintln("client:" + msg);

}

} catch (IOException e) {

eprintStackTrace();

} finally {

try {

if (br != null)

brclose();

if (socket != null)

socketclose();

} catch (IOException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

}

}

private BufferedReader getReader() {

try {

return new BufferedReader(new InputStreamReader(socket

getInputStream()));

} catch (IOException e) {

eprintStackTrace();

}

return null;

}

}

class ClientWriter implements Runnable {

private Socket socket;

public ClientWriter(Socket socket) {

thissocket = socket;

}

public void run() {

PrintWriter pw = getWriter();

BufferedReader br = new BufferedReader(new InputStreamReader(Systemin));

String str = null;

try {

while ((str = brreadLine()) != null) {

pwprintln(str);

pwflush();

}

} catch (IOException e) {

eprintStackTrace();

} finally {

if (pw != null)

pwclose();

try {

if (socket != null)

socketclose();

} catch (IOException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

}

}

private PrintWriter getWriter() {

try {

return new PrintWriter(socketgetOutputStream());

} catch (IOException e) {

eprintStackTrace();

}

return null;

}

}

SocketServer1java内容如下:

package compolyphyllasocket;

import javaioBufferedReader;

import javaioIOException;

import javaioInputStreamReader;

import javaioPrintWriter;

import javanetServerSocket;

import javanetSocket;

public class SocketServer1 {

private ServerSocket ss;

public SocketServer1() {

try {

ss = new ServerSocket(2222);

} catch (IOException e) {

eprintStackTrace();

}

}

public static void main(String[] args) {

new SocketServer1()startServer();

}

public void startServer() {

try {

while (true) {

Socket s = ssaccept();

new Thread(new ServerReader(s))start();

new Thread(new ServerWriter(s))start();

}

} catch (IOException e) {

eprintStackTrace();

}

}

}

class ServerReader implements Runnable {

private Socket socket;

public ServerReader(Socket socket) {

thissocket = socket;

}

public void run() {

BufferedReader br = getReader();

String msg = null;

try {

while((msg = brreadLine()) != null) {

Systemoutprintln("server:" + msg);

}

} catch (IOException e) {

eprintStackTrace();

} finally {

try {

if(br != null)

brclose();

if(socket != null)

socketclose();

} catch (IOException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

}

}

private BufferedReader getReader() {

try {

return new BufferedReader(new InputStreamReader(socketgetInputStream()));

} catch (IOException e) {

eprintStackTrace();

}

return null;

}

}

class ServerWriter implements Runnable {

private Socket socket;

public ServerWriter(Socket socket) {

thissocket = socket;

}

public void run() {

PrintWriter pw = getWriter();

BufferedReader br = new BufferedReader(new InputStreamReader(Systemin));

String str = null;

try {

while((str = brreadLine()) != null) {

pwprintln(str);

pwflush();

}

} catch (IOException e) {

eprintStackTrace();

} finally {

if (pw != null)

pwclose();

try {

if (socket != null)

socketclose();

} catch (IOException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

}

}

private PrintWriter getWriter() {

try {

return new PrintWriter(socketgetOutputStream());

} catch (IOException e) {

eprintStackTrace();

}

return null;

}

}

DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
网站模板库 » socket双向通信,为什么服务器只能接收一次客户端发送的消息

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情