socket java实现客户端多线程接受消息并发送消息给服务器,并发执行 5
3个回答
展开全部
客服端:
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class KeFuDuan {
public static void main(String[] args) {
KeFuDuan kf = new KeFuDuan();
kf.start();
}
public void start(){
Socket sco;
String ss= "";
try {
sco = new Socket("127.0.0.1",8866);
KeFuduanJie kf = new KeFuduanJie(sco);
KeFuWuFasong kfs = new KeFuWuFasong(sco);
kf.start();
kfs.start();
//sco.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class KeFuduanJie extends Thread{
Socket soc;
String ss;
BufferedReader br;
public KeFuduanJie(Socket soc){
try {
this.soc = soc;
br = new BufferedReader(new InputStreamReader(soc.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
//负责接受服务端来的信息
public void run(){
while(true){
//接受服务器端来的信息
try {
ss = br.readLine();
System.out.println("服务器---->客服端: "+ss);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//向服务器发送东西
class KeFuWuFasong extends Thread{
Socket soc;
BufferedWriter bw;
BufferedReader brr;
public KeFuWuFasong(Socket soc){
this.soc = soc;
try {
brr =new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(soc.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
//向服务器发送请求
try {
bw.write(brr.readLine());
bw.newLine();
bw.flush();// 或者用bw.close()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
服务器端:
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
Server server = new Server();
server.start();
}
public void start(){
try { //服务器端打开端口
ServerSocket server = new ServerSocket(4499);
Socket socket = null;
ServerToClientThread stct = null;
while(true){
socket = server.accept(); //迎接(接收)客户端的Socket访问
stct = new ServerToClientThread(socket); //分配一个新线程负责和信赖的Socket沟通
stct.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class ServerReceiveFromClient extends Thread{
Socket socket;
BufferedReader br;
String s;
public ServerReceiveFromClient(Socket socket){
this.socket = socket;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
while(true){
try {
s = br.readLine();
System.out.println(socket.getInetAddress().getHostAddress()+"发送了:"+s);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class ServerToClientThread extends Thread{
Socket socket;
BufferedReader br;
BufferedWriter bw;
String s;
//建立的同时,和客户端的Socket建立输入、输出流
public ServerToClientThread(Socket socket){
this.socket = socket;
try {
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
ServerReceiveFromClient srfc = new ServerReceiveFromClient(socket);
srfc.start();
while(true){
try {
bw.write("欢迎光临。");
bw.newLine();
bw.flush();
Thread.sleep(10*1000);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class KeFuDuan {
public static void main(String[] args) {
KeFuDuan kf = new KeFuDuan();
kf.start();
}
public void start(){
Socket sco;
String ss= "";
try {
sco = new Socket("127.0.0.1",8866);
KeFuduanJie kf = new KeFuduanJie(sco);
KeFuWuFasong kfs = new KeFuWuFasong(sco);
kf.start();
kfs.start();
//sco.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class KeFuduanJie extends Thread{
Socket soc;
String ss;
BufferedReader br;
public KeFuduanJie(Socket soc){
try {
this.soc = soc;
br = new BufferedReader(new InputStreamReader(soc.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
//负责接受服务端来的信息
public void run(){
while(true){
//接受服务器端来的信息
try {
ss = br.readLine();
System.out.println("服务器---->客服端: "+ss);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//向服务器发送东西
class KeFuWuFasong extends Thread{
Socket soc;
BufferedWriter bw;
BufferedReader brr;
public KeFuWuFasong(Socket soc){
this.soc = soc;
try {
brr =new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(soc.getOutputStream()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run(){
while(true){
//向服务器发送请求
try {
bw.write(brr.readLine());
bw.newLine();
bw.flush();// 或者用bw.close()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
服务器端:
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
Server server = new Server();
server.start();
}
public void start(){
try { //服务器端打开端口
ServerSocket server = new ServerSocket(4499);
Socket socket = null;
ServerToClientThread stct = null;
while(true){
socket = server.accept(); //迎接(接收)客户端的Socket访问
stct = new ServerToClientThread(socket); //分配一个新线程负责和信赖的Socket沟通
stct.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class ServerReceiveFromClient extends Thread{
Socket socket;
BufferedReader br;
String s;
public ServerReceiveFromClient(Socket socket){
this.socket = socket;
try {
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
while(true){
try {
s = br.readLine();
System.out.println(socket.getInetAddress().getHostAddress()+"发送了:"+s);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package MyKeFudaun;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class ServerToClientThread extends Thread{
Socket socket;
BufferedReader br;
BufferedWriter bw;
String s;
//建立的同时,和客户端的Socket建立输入、输出流
public ServerToClientThread(Socket socket){
this.socket = socket;
try {
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
ServerReceiveFromClient srfc = new ServerReceiveFromClient(socket);
srfc.start();
while(true){
try {
bw.write("欢迎光临。");
bw.newLine();
bw.flush();
Thread.sleep(10*1000);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2012-03-02
展开全部
对于通信来说,不存在绝对的服务器和客户端,谁在等待别人来,谁就是服务器,谁主动去联系人,谁就是客户端。
所以。
你要想客户端接受消息,那在启动客户端的时候,在客户端程序里开始一个提供端口的Socket就可以了。
ServerSocket serverSocket = new ServerSocket(5000);
while (true) {
final Socket socket = serverSocket.accept();
new Thread() {
Socket mySocket = socket;
@Override
public void run() {
try {
System.out.println(mySocket);
InputStream is = mySocket.getInputStream();
byte[] bytes = new byte[1024];
int n = is.read(bytes);
System.out.println(new String(bytes, 0, n));
OutputStream os = mySocket.getOutputStream();
os.write(("server reply at time " + new Date()
.toString()).getBytes());
mySocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
所以。
你要想客户端接受消息,那在启动客户端的时候,在客户端程序里开始一个提供端口的Socket就可以了。
ServerSocket serverSocket = new ServerSocket(5000);
while (true) {
final Socket socket = serverSocket.accept();
new Thread() {
Socket mySocket = socket;
@Override
public void run() {
try {
System.out.println(mySocket);
InputStream is = mySocket.getInputStream();
byte[] bytes = new byte[1024];
int n = is.read(bytes);
System.out.println(new String(bytes, 0, n));
OutputStream os = mySocket.getOutputStream();
os.write(("server reply at time " + new Date()
.toString()).getBytes());
mySocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
更多追问追答
追问
客户端要接收服务器发出的消息,并在接收消息的过程中发送给服务器消息,你那个不行吧,
追答
这个不是完整代码,否则你的问题还涉及到一个真实的服务器端,客户端的代码,我写的这个只是在客户端启动的时候开启的一个服务监听程序。当服务器往这个端口发请求时,这个请求就会给出相应的服务。要理解,对于Socket来说客户端是针对提供服务的服务器端说的,可以放在真实的服务器端,也能放在客户端。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
自己写的socket通信在遇到大的并发量时经常会出现各种各样的问题,对于你的这种需求,建议你使用apache下面的mina框架来写,很简单的,就是专门的网络通讯框架,里面的socket
封装的比较完善,没啥大问题!
封装的比较完善,没啥大问题!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询