45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:如何通过java socket实现服务端与客户端?

如何通过java socket实现服务端与客户端?

2016-04-02 12:18:32 来源:www.45fan.com 【

如何通过java socket实现服务端与客户端?

一对一的服务端和客户机:一个服务端只能同时服务一个客户端
 
服务端:
 
import java.io.*;
 
import java.net.*;
 
public class MyServer {
 
public static void main(String[] args) throws IOException{
 
ServerSocket server=new ServerSocket(5678);
 
Socket client=server.accept();
 
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
 
PrintWriter out=new PrintWriter(client.getOutputStream());
 
while(true){
 
String str=in.readLine();
 
System.out.println(str);
 
out.println("has receive....");
 
out.flush();
 
if(str.equals("end"))
 
break;
 
}
 
client.close();
 
}
 
}
 
accept()方法说明:
 
public Socket accept() throws IOException
 
Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.
 
A new Socket s is created and, if there is a security manager, the security manager‘s checkAccept method is called with s.getInetAddress().getHostAddress() and s.getPort() as its arguments to ensure the operation is allowed. This could result in a SecurityException.
 
客户端
 
import java.net.*;
 
import java.io.*;
 
public class Client{
 
static Socket server;
 
public static void main(String[] args)throws Exception{
 
server=new Socket(InetAddress.getLocalHost(),5678);
 
BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
 
PrintWriter out=new PrintWriter(server.getOutputStream());
 
//标准输入
 
BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
 
while(true){
 
String str=wt.readLine();
 
out.println(str);
 
out.flush();
 
if(str.equals("end")){
 
break;
 
}
 
System.out.println(in.readLine());
 
}
 
server.close();
 
}
 
}
 
一对多的服务端和客户机:一个服务端只能同时服务多个客户端,需要使用多线程来实现
 
服务端:调用具体的处理线程完成处理
 
import java.io.*;
 
import java.net.*;
 
public class MyMultiServer {
 
public static void main(String[] args) throws IOException{
 
ServerSocket server=new ServerSocket(5678);
 
while (true){
 
Socket client=server.accept();
 
ChildTh child=new ChildTh(client);//用socket实例初始化具体的处理线程对象
 
//使用Runnable接口和使用extends Thread的区别:
 
// 如果是使用Runnable接口,需要这么写:
 
new Thread(child).start();
 
//注意:不能写成:child.run();
 
// 如果使用extends Thread,只需要child.start()就可以,因为start()是Thread的方法
 
}
 
}
 
}
 
处理线程类:
 
import java.io.*;
 
import java.net.*;
 
public class ChildTh implements Runnable{
 
private Socket client;
 
public ChildTh(Socket client){
 
this.client=client;
 
}
 
public void run(){
 
try{
 
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
 
PrintWriter out=new PrintWriter(client.getOutputStream());
 
while(true){
 
String str=in.readLine();
 
System.out.println(str);
 
out.println("has receive....");
 
out.flush();
 
if(str.equals("end"))
 
break;
 
}
 
client.close();
 
}catch(Exception e){
 
System.out.println("error in the close the socket!");
 
e.printStackTrace();
 
}
 
finally{
 
}
 
}
 
}
 
客户端:
 
import java.net.*;
 
import java.io.*;
 
public class Client{
 
static Socket server;
 
public static void main(String[] args)throws Exception{
 
server=new Socket(InetAddress.getLocalHost(),5678);
 
BufferedReader in=new BufferedReader(new InputStreamReader(server.getInputStream()));
 
PrintWriter out=new PrintWriter(server.getOutputStream());
 
//标准输入
 
BufferedReader wt=new BufferedReader(new InputStreamReader(System.in));
 
while(true){
 
String str=wt.readLine();
 
out.println(str);
 
out.flush();
 
if(str.equals("end")){
 
break;
 
}
 
System.out.println(in.readLine());
 
}
 
server.close();
 
}
 
}
 

本文地址:http://www.45fan.com/a/question/44618.html
Tags: 实现 Java Socket
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部