2、实现服务器端和客户端程序
服务器端
public class ServerSocket extends Object
ServerSocket(int port)
创建绑定到特定端口的服务器套接字
void setSoTimeout(int timeout)
通过制定超市值启用/禁用SO_TIMEIOUT 以毫秒为单位
InetAddress getInetAddress()
返回此服务器套接字的本地地址
Socket accept()
帧听并接受此套接字的连接
* 实现服务器端与客户端程序
* 客户端
```
public class Socket extends Object
此类实现客户端套接字(也可以就“套接字”),套接字是两台机器间通信的端点
Socket(String host,int port)
创建一个流套接字并将其连接到指定主机上的指定端口号
InputStream getInputStream()
返回次套接字的输入流
OutputStream getOutputStream()
返回此套接字的输出流
void setSoTimeout(int timeout)
启用/禁用带有指定超时值得SO_TIMEOUT,以毫秒为单位
```
服务端步奏:
//服务器端
public static void main(String[] args) {
// 1024--65535
try {
//1、创建ServerSocket对象,绑定监听端口
ServerSocket ss = new ServerSocket(8888);
System.out.println("服务器已经启动,正在等待连接。。。");
//2、通过accept()方法监听客户端的请求
Socket socket = ss.accept(); // 表示客户端等待连接 会阻塞
//3、连接建立后,通过输入流读取客户端发送的请求信息
InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String info = br.readLine();
System.out.println(info);
//4、通过输出流向客户端发送响应的信息
OutputStream out = socket.getOutputStream();
PrintStream ps = new PrintStream(out);
ps.println("demo" + info);
//5、关闭相应的资源
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//客户端程序
// 连接服务器
try {
//创建Socket对象,指明需要连接的服务器的地址和端口号
Socket socket = new Socket("localhost", 8888);
System.out.println("连接成功");
// 输出流 连接建立后,通过输出流向服务器发送请求信息
OutputStream out = socket.getOutputStream();
// 输入流 通过输入流获取服务器响应的信息
InputStream in = socket.getInputStream();
PrintStream ps = new PrintStream(out);
ps.println("hello");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
System.out.println(br.readLine());
//关闭相应的资源
out.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
目前为止我们编写的程序中,服务器只能处理一个客户端连接,要想服务器同时支持多个客户端的连接,就必须加入多线程的处理机制,将每一个连接的客户端创建一个新的线程对象。
应用多线程来实现服务器与多客户端之间的通信
// 服务器端代码
package com.yinlei.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.omg.CORBA.TRANSACTION_REQUIRED;
public class MutilServer {
public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();
try {
ServerSocket ss = new ServerSocket(8000);
System.out.println("服务器已经启动,正在等待连接。。。");
while (true) {
Socket socket = ss.accept(); // 等待客户端连接,阻塞
es.execute(new ClientThread(socket));
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class ClientThread implements Runnable {
private Socket socket;
public ClientThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
System.out.println("客户端的IP为:" + socket.getInetAddress().getHostName());
InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
OutputStream out = socket.getOutputStream();
PrintStream ps = new PrintStream(out);
while (true) {
String info = br.readLine();
if (info==null || "bye".equals("bye")) {
break;
}
System.out.println(info);
ps.println("demo : " + info);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//客户端代码
package com.yinlei.socket;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
public class MutilSocket {
public static void main(String[] args) {
// 连接服务器
try {
Socket socket = new Socket("localhost", 8000);
System.out.println("连接成功");
boolean flag = true;
// 输出流
OutputStream out = socket.getOutputStream();
// 输入流
InputStream in = socket.getInputStream();
PrintStream ps = new PrintStream(out);
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.println("请输入...");
while (flag) {
String info = input.next();
if ("bye".equals(info)) {
flag = false;
break;
}
ps.println(info);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
info = br.readLine();
System.out.println(info);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//服务器端接收数据
//客户端接收数据,有bye的时候退出
URL url = new URL("");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c:\\dasd.jpg"));
byte[]bs = new byte[1024];
int len = -1;
while((len = bis.read(bs)) != -1){
bos.write(bs,0,len);
}
bos.close();
bis.close();
System.out.println("success");
下载一个网络图片并且显示;
new Thread() {
//异步下载
@Override
public void run() {
try {
URL url = new URL(image);
try {
InputStream is = url.openStream();
final Bitmap bitmap = BitmapFactory.decodeStream(is);
runOnUiThread(new Runnable() {
//主线程中更新UI
@Override
public void run() {
mImageView.setImageBitmap(bitmap);
}
});
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
Http通信中,的POST和GET请求方式不同,GET把参数放在URL字符串的后面,传递给服务器,而POST方法的参数是放在http请求中,因此,在编程之前,应当首先明确使用的请求方法,然后在依据使用的方法选择相应的编程方式
HttpURLConnection是继承于URLConnection类,二者都是抽象类,其对象主要通过URL的openConnection方法获得
try {
HttpURLConnection conn = (HttpURLConnection) new URL(image).openConnection();
conn.setDoOutput(true); //post情况下需要设置为true,默认是false
conn.setDoInput(true); //设置是否从httpUrlConnection读入,默认是true
conn.setRequestMethod("POST");
conn.setUseCaches(true); //设置是否用缓存,post请求不用缓存
//设置content-type
conn.setConnectTimeout(3000); //设置连接超时
conn.setReadTimeout(3000); //设置读取超时
//获取输出流,便于想服务器发送新消息
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
//往流里面写请求参数
dos.writeBytes("name=" + URLEncoder.encode("hello","gb2312"));
dos.flush();
dos.close();
//获取输入流,取数据
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
br.readLine(); //用 !=null 来判断是否结束
br.close();
//关闭connection
conn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
相关操作类:
服务器端实现步骤:
// 1、创建DatagragmSocket,指定端口号
DatagramSocket socket = null;
try {
socket = new DatagramSocket(8888);
} catch (SocketException e) {
e.printStackTrace();
}
// 创建字节数组,用于接收客户端发送的数据
byte[] data = new byte[1024];
// 2、创建DatagramPacket
DatagramPacket packet = new DatagramPacket(data, data.length);
// 3、接收客户端发送的数据信息
try {
socket.receive(packet); //此方法在接收到数据报之前会一直阻塞
} catch (IOException e) {
e.printStackTrace();
}
// 4、读取数据
String info = new String(data, 0, packet.getLength());
System.out.println("我是服务器---》"+ "客户端说:" +info);
//-----------------------------------------------------------------------------
/** * 向客户端响应数据 */
//定义客户端的地址、端口号、数据
InetAddress address = null;
address = packet.getAddress();
int port = packet.getPort();
byte[]data1= "欢迎您".getBytes();
//创建数据报,包含响应的数据信息
DatagramPacket packet2 = new DatagramPacket(data1, data1.length,address,port);
DatagramSocket socket2 = null;
try {
socket2 = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
try {
socket2.send(packet2);
} catch (IOException e) {
e.printStackTrace();
}
//关闭资源
socket.close();
socket2.close();
// 1、创建DatagragmSocket,指定端口号
DatagramSocket socket = null;
try {
socket = new DatagramSocket(8888);
} catch (SocketException e) {
e.printStackTrace();
}
// 创建字节数组,用于接收客户端发送的数据
byte[] data = new byte[1024];
// 2、创建DatagramPacket
DatagramPacket packet = new DatagramPacket(data, data.length);
// 3、接收客户端发送的数据信息
try {
socket.receive(packet); //此方法在接收到数据报之前会一直阻塞
} catch (IOException e) {
e.printStackTrace();
}
// 4、读取数据
String info = new String(data, 0, packet.getLength());
System.out.println("我是服务器---》"+ "客户端说:" +info);
//-----------------------------------------------------------------------------
/** * 向客户端响应数据 */
//定义客户端的地址、端口号、数据
InetAddress address = null;
address = packet.getAddress();
int port = packet.getPort();
byte[]data1= "欢迎您".getBytes();
//创建数据报,包含响应的数据信息
DatagramPacket packet2 = new DatagramPacket(data1, data1.length,address,port);
DatagramSocket socket2 = null;
try {
socket2 = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
try {
socket2.send(packet2);
} catch (IOException e) {
e.printStackTrace();
}
//关闭资源
socket.close();
socket2.close();
总结:
* 是否关闭输出流和输入流
* 对于同一个socket,如果关闭了输出流,则与改输出流相关的socket也会被关闭,所以一般不关闭流,直接关闭socket即可
* 传入传出对象、文件—->输入输出流