通过Java中的Socket技术编写能够进行双向通信的客户端和服务器,为了能够同时进行消息的发送和接受,在服

求一答案!... 求一答案! 展开
 我来答
Polyphylla1022
2011-10-09 · TA获得超过705个赞
知道小有建树答主
回答量:187
采纳率:0%
帮助的人:150万
展开全部
SocketClient1.java内容如下:

package com.polyphylla.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

public class SocketClient1 {
private Socket socket;

public SocketClient1() {
try {
socket = new Socket("127.0.0.1", 2222);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* @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) {
this.socket = socket;
}

public void run() {
BufferedReader br = getReader();
String msg = null;
try {
while ((msg = br.readLine()) != null) {
System.out.println("client:" + msg);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (socket != null)
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

private BufferedReader getReader() {
try {
return new BufferedReader(new InputStreamReader(socket
.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

class ClientWriter implements Runnable {
private Socket socket;

public ClientWriter(Socket socket) {
this.socket = socket;
}

public void run() {
PrintWriter pw = getWriter();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
try {
while ((str = br.readLine()) != null) {
pw.println(str);
pw.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null)
pw.close();
try {
if (socket != null)
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

private PrintWriter getWriter() {
try {
return new PrintWriter(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
SocketServer1.java内容如下:
package com.polyphylla.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServer1 {
private ServerSocket ss;
public SocketServer1() {
try {
ss = new ServerSocket(2222);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new SocketServer1().startServer();
}

public void startServer() {
try {
while (true) {
Socket s = ss.accept();
new Thread(new ServerReader(s)).start();
new Thread(new ServerWriter(s)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
class ServerReader implements Runnable {
private Socket socket;
public ServerReader(Socket socket) {
this.socket = socket;
}
public void run() {
BufferedReader br = getReader();
String msg = null;
try {
while((msg = br.readLine()) != null) {
System.out.println("server:" + msg);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null)
br.close();
if(socket != null)
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private BufferedReader getReader() {
try {
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
class ServerWriter implements Runnable {
private Socket socket;
public ServerWriter(Socket socket) {
this.socket = socket;
}
public void run() {
PrintWriter pw = getWriter();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
try {
while((str = br.readLine()) != null) {
pw.println(str);
pw.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null)
pw.close();
try {
if (socket != null)
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private PrintWriter getWriter() {
try {
return new PrintWriter(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式