java socket 服务端_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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值