网络编程(理解)
A:IP地址
a:点分十进制
b:IP地址的组成
c:IP地址的分类
d:dos命令
e:InetAddress
B:端口
是应用程序的标识。范围:0-65535,其中0-1024不建议使用(系统端口)
C:协议
UDP:数据打包、有限制、无连接、效率高、不可靠
TCP:建立数据通道、无限制
A:通信两端都应该有Socket对象
B:所有的通信都是通过Socket间IO进行操作的
发送:
创建UDP发送端的Socket对象
创建数据并把数据打包
发送数据
释放资源
接收:
创建UDP接收端的Socket对象
创建数据包用于接收数据
接收数据
解析数据包
释放资源
package day26_UDP;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/*
* UDP协议发送数据:
* A:创建发送端Scoket对象
* B:创建数据,并把数据打包
* C:调用Socket对象的发送方法发送数据包
* D:释放资源
* */
public class SendDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建发送端Scoket对象
//DatagramSocket
DatagramSocket ds = new DatagramSocket();
//创建数据,并把数据打包
//DatagramPacket(byte[]buf,int length,InetAddress address,int port)
//创建数据
byte[]bys="hello world!".getBytes();
int length=bys.length;
//IP地址对象
InetAddress address = InetAddress.getByName("192.168.1.104");
//端口
int port=10086;
DatagramPacket p = new DatagramPacket(bys, length, address, port);
//调用Socket对象的发送方法发送数据包
//public void send(DatagramSocket p)
ds.send(p);
//释放资源
ds.close();
}
}
package day26_UDP;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/*
* UDP协议接收数据:
* A:创建发送端Scoket对象
* B:创建一个数据包(接收容器)
* C:调用Socket对象的接收方法接收数据
* D:解析数据,并显示在控制台
* E:释放资源
*
* */
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// 创建发送端Scoket对象
// DatagramSocket(int port)
DatagramSocket ds = new DatagramSocket(10085);
// 创建一个数据包(接收容器)
// DatagramPacket(byte[] buf,int length)
byte[] bys = new byte[1024];
int length = bys.length;
DatagramPacket dp = new DatagramPacket(bys, length);
// 调用Socket对象的接收方法接收数据
ds.receive(dp);
// 解析数据,并显示在控制台
// 获取对方的ip
// public InetAddress getAddress()
InetAddress address = dp.getAddress();
String ip = address.getHostAddress();
// public byte[] getData():获取数据缓冲区
// public int getLength():获取数据的实际长度
byte[] bys2 = dp.getData();
int len = dp.getLength();
String s = new String(bys2, 0, len);
System.out.println(ip + ":" + s);
// 释放资源
ds.close();
}
}
发送:
创建TCP客户端的Socket对象
获取输出流,写数据
释放资源
接收:
创建TCP服务器端的Socket对象
监听客户端对象
获取输入流,读取数据
释放资源
package day26_TCP;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
/*
* TCP发送数据:
* A:创建发送端的Socket对象、
* 这一步如果成功,就说明连接已经建立成功了
* B:获取输出流,写数据
* C:释放资源
*
* 连接被拒绝,TCP协议一定要先开服务器
* java.net.ConnectException: Connection refused: connect
*
* */
public class ClientDemo {
public static void main(String[] args) throws IOException, IOException {
// TODO Auto-generated method stub
//创建Socket对象
//Socket(InetAddress address, int port)
//Socket(String host,int port)
//Socket socket = new Socket(InetAddress.getByName("192.168.1.104"),8888);
Socket s=new Socket("192.168.1.104", 8888);
//获取输出流
//public OutputStream getOutputStream()
OutputStream os = s.getOutputStream();
os.write("hello,tcp".getBytes());
//释放资源
s.close();
}
}
package day26_TCP;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*
* TCP协议接收数据:
* A:创建接收端的Socket对象
* B:监听客户端
* C:获取输入流,读取数据显示在控制台
* D:释放资源
* */
public class ServerDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建接收端的Socket的对象
//ServerSocket(int port)
ServerSocket ss = new ServerSocket(8888);
//监听客户端,返回一个对应的Socket对象
//public Socket accept()
Socket s = ss.accept();//阻塞式方法
//获取输入流,读取数据显示在控制台
InputStream is = s.getInputStream();
byte[]bys=new byte[1024];
int len=is.read(bys);
String str=new String(bys, 0, len);
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+":"+str);
//释放资源
//关闭socket对象
//不关闭服务器
s.close();
// ss.close();//这个不关闭
}
}
A:UDP
a:最基本的UPD协议发送和接收数据
package day26_UDP优化;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class SendDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建发送端Socket对象
DatagramSocket ds = new DatagramSocket();
//创建数据并打包
byte[]bys="hellowrold".getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length,InetAddress.getByName("192.168.1.104"),12344);
//发送数据
ds.send(dp);
//释放资源
ds.close();
}
}
package day26_UDP优化;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/*
* 多次启动接收端:
* java.net.BingException:Address already in use:Cannot bind
* */
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// 创建接收端的Socket对象
DatagramSocket ds = new DatagramSocket(12344);
// 创建一个包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
// 接收数据
ds.receive(dp);
// 解析数据
String ip = dp.getAddress().getHostAddress();
String s = new String(dp.getData(), 0, dp.getLength());
System.out.println("from" + ip + "data is:" + s);
//释放资源
ds.close();
}
}
package day26_UDPDemo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
/*
* 数据来自与键盘录入
* 键盘录入数据要自己控制录入结束(886)
* */
public class SendDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建发送端Socket对象
DatagramSocket ds = new DatagramSocket();
//封装键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=br.readLine())!=null){
if("886".equals(line)){
break;
}
//创建数据并打包
byte[]bys=line.getBytes();
DatagramPacket dp = new DatagramPacket(bys, bys.length,InetAddress.getByName("192.168.1.104"),12341);
//发送数据
ds.send(dp);
}
//释放资源
ds.close();
}
}
package day26_UDPDemo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/*
* 多次启动接收端:
* java.net.BingException:Address already in use:Cannot bind
* 端口被占用
* */
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// 创建接收端的Socket对象
DatagramSocket ds = new DatagramSocket(12341);
while(true){
// 创建一个包
byte[] bys = new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
// 接收数据
ds.receive(dp);
// 解析数据
String ip = dp.getAddress().getHostAddress();
String s = new String(dp.getData(), 0, dp.getLength());
System.out.println("from" + ip + "data is:" + s);
}
//释放资源
//接收端应该一直开着等待接收数据,是不需要关闭的
// ds.close();
}
}
package day26_UDPDemo2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class SendThread implements Runnable {
private DatagramSocket ds;
public SendThread(DatagramSocket ds) {
// TODO Auto-generated constructor stub
this.ds=ds;
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
//从键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=br.readLine())!=null){
//如果键盘录入时886,则结束
if(line.equals("886")){
break;
}
//创建数据打包
byte[]bys=line.getBytes();
InetAddress address = InetAddress.getByName("192.168.1.104");
DatagramPacket dp = new DatagramPacket(bys, bys.length, address, 12306);
//发送数据
ds.send(dp);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
package day26_UDPDemo2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ReceiveThread implements Runnable {
private DatagramSocket ds;
public ReceiveThread(DatagramSocket ds) {
// TODO Auto-generated constructor stub
this.ds=ds;
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
while(true){
//创建数据包
byte[]bys=new byte[1024];
DatagramPacket dp = new DatagramPacket(bys, bys.length);
//接收数据
ds.receive(dp);
//解析数据
String hostIp = dp.getAddress().getHostAddress();
byte[] data = dp.getData();
String s = new String(data,0,data.length);
System.out.println("From"+hostIp+"data is :"+s);
}
}catch(IOException e){
e.printStackTrace();
}
}
}
package day26_UDPDemo2;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.SocketException;
/*
* 通过多线程改进刚才的聊天程序,这样我就可以实现在一个窗口发送和接收数据了
* */
public class ChatRoom {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建Socket套接字
DatagramSocket sendDs = new DatagramSocket();
DatagramSocket receiveDs = new DatagramSocket(12306);
//创建两个线程实现,创建对象
SendThread st = new SendThread(sendDs);
ReceiveThread rt = new ReceiveThread(receiveDs);
//实现线程实例,使用Runnable实现
Thread t1 = new Thread(st);
Thread t2 = new Thread(rt);
//启动线程
t1.start();
t2.start();
}
}
B:TCP
a:最基本的TCP协议发送和接收数据
b:服务器给出反馈
package day26_TCP优化;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class ClientDemo {
public static void main(String[] args) throws IOException, IOException {
// TODO Auto-generated method stub
//创建客户端Socket对象
Socket s = new Socket("192.168.1.104",9999);
//获取输出流
OutputStream os = s.getOutputStream();
os.write("hellowrold".getBytes());
//获取输入流
InputStream is = s.getInputStream();
byte[]bys=new byte[1024];
int len = is.read(bys);
String client=new String(bys, 0, len);
System.out.println("client:"+client);
//释放资源
s.close();
}
}
package day26_TCP优化;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建服务器Socket对象
ServerSocket ss = new ServerSocket(9999);
//监听客户端连接
Socket s = ss.accept();
//获取输入流
InputStream is=s.getInputStream();
byte[]bys=new byte[1024];
int len=is.read(bys);
String server = new String(bys,0,len);
System.out.println("server:"+server);
//获取输出流
OutputStream os = s.getOutputStream();
os.write("数据已经收到".getBytes());
//释放资源
s.close();
// ss.close();//这个可以不关
}
}
package day26_TCPDemo;
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 ClientDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建Socket对象
//ip地址可能会变
Socket s = new Socket("192.168.1.100",22222);
//使用高效数据流键盘录入数据
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//给通道写入数据
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line=null;
while((line=br.readLine())!=null){
//键盘输入886结束
if(line.equals("886")){
break;
}
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
//br.close();//键盘录入886后就结束了
//bw.close();//s关闭后,bw就关闭了
s.close();
}
}
package day26_TCPDemo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建ServerSocket对象
ServerSocket ss = new ServerSocket(22222);
//客户端监听
Socket s = ss.accept();
//包装通道内容的流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
//释放资源
s.close();
}
}
package day26_TCPDemo2;
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 ClientDemo {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
//创建Socket对象
Socket s = new Socket("192.168.1.100",23456);
//获取键盘录入数据流
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//封装键盘录入数据到通道(发送)
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line=null;
while((line=br.readLine())!=null){
//结束标记
if(line.equals("over")){
break;
}
//写入通道
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
// br.close();
// bw.close();
s.close();
}
}
package day26_TCPDemo2;
/*
* 客户键盘录入,服务器输出文本文件
* */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
// 创建ServerSocket对象
ServerSocket ss = new ServerSocket(23456);
//监听用户对象
Socket s = ss.accept();
//读取通道数据
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//封装文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("Tcp.txt"));
//写入文本
String line=null;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
bw.close();//文件操作流需要关闭
s.close();
}
}
package day26_TCPDemo3;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
/*
* 客户端文本文件,服务器输出到控制台
* */
public class ClientDemo {
public static void main(String[] args)throws IOException {
//创建Socket对象
Socket s = new Socket("192.168.1.100",34567);
//封装文本文件
BufferedReader br = new BufferedReader(new FileReader("Tcp.txt"));
//封装通道内的流(客户端写)
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String line=null;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
br.close();
s.close();
}
}
package day26_TCPDemo3;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
//创建ServerSocket对象
ServerSocket ss = new ServerSocket(34567);
//监听客户端
Socket s = ss.accept();
//封装通道流数据(服务器读)
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
//释放资源
s.close();
}
}
package day26_TCPDemo4;
/*
* 需求:客户端上传文件,服务器端输出文本文件
*
* 按照我们正常的思路加入反馈信息,结果却没有反应,为什么呢?
* 读取文本文件是可以null作为结束信息的,但是呢?通道内是没有这种方式结束信息de
* 所以服务器根本就不知道你结束了,而你还想服务器给你反馈,所以相互等待了。
*
* */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class UploadClient {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
//创建Socket对象
Socket s = new Socket("192.168.1.100",11111);
//封装文本文件
BufferedReader br = new BufferedReader(new FileReader("Tcp.txt"));
//封装通道内流(写)
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//写数据
String line=null;
while((line=br.readLine())!=null){//阻塞
bw.write(line);
bw.newLine();
bw.flush();
}
//Socket提供了一个终止功能,会通知服务器你别等了,
s.shutdownOutput();
//接收反馈
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String client = brClient.readLine();//阻塞
System.out.println(client);
//释放资源
br.close();
s.close();
}
}
package day26_TCPDemo4;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class UploadServer {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
//创建ServerSocket对象
ServerSocket ss = new ServerSocket(11111);
//监听客户端
Socket s = ss.accept();//阻塞
//封装通道内流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//封装文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("Tcp_Copy.txt"));
//写入数据
String line=null;
while((line=br.readLine())!=null){//阻塞
bw.write(line);
bw.newLine();
bw.flush();
}
//给出反馈
BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上传成功");//阻塞
bwServer.newLine();
bwServer.flush();
//释放资源
bw.close();
s.close();
}
}
package day26_TCPDemo5;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class UploadClient {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
//创建客户Socket对象
Socket s = new Socket("192.168.1.100",19191);
//封装图片文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("Test.jpg"));
//封装通道内流
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
//将图片写入通道
byte[]bys=new byte[1024];
int len=0;
while((len=bis.read(bys))!=-1){
bos.write(bys, 0, len);
//刷新
bos.flush();
}
//通知服务器结束
s.shutdownOutput();
//接收反馈
InputStream is = s.getInputStream();
byte[]bys2=new byte[1024];
int len2=is.read(bys2);
String client=new String(bys2, 0, len2);
System.out.println(client);
//释放资源
bis.close();
s.close();
}
}
package day26_TCPDemo5;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/*
* 上传图片
* */
public class UploadServer {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
//创建ServerSocket对象
ServerSocket ss = new ServerSocket(19191);
//监听客户连接
Socket s = ss.accept();
//封装通道内流
//图片用字节流(字节高效流)
BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
//封装图片文件
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Test_Copy.jpg"));
//写入通道
byte[]bys=new byte[1024];
int len=0;
while((len=bis.read(bys))!=-1){
bos.write(bys, 0, len);
//刷新
bos.flush();
}
//给个反馈
OutputStream os = s.getOutputStream();
os.write("图片上传成功".getBytes());
//释放资源
bos.close();
s.close();
}
}
package day26_Multi_Tcp;
/*
* 通过while循环可以改进一个服务器接收多个客户端
* 但是这个是有问题的
* 如果是这样的情况,假设我有张三、李四等多个人分别执行客户端
* 张三:tcp.txt 1M
* 李四:b.txt 100K
* 等等
* */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class UploadClient {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
//创建Socket对象
Socket s = new Socket("192.168.1.100",11111);
//封装文本文件
//张三
//BufferedReader br = new BufferedReader(new FileReader("Tcp.txt"));
//李四
BufferedReader br = new BufferedReader(new FileReader("b.txt"));
//封装通道内流(写)
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//写数据
String line=null;
while((line=br.readLine())!=null){//阻塞
bw.write(line);
bw.newLine();
bw.flush();
}
//Socket提供了一个终止功能,会通知服务器你别等了,
s.shutdownOutput();
//接收反馈
BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
String client = brClient.readLine();//阻塞
System.out.println(client);
//释放资源
br.close();
s.close();
}
}
package day26_Multi_Tcp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class UserThread implements Runnable {
private Socket s;
//构造方法
public UserThread(Socket s) {
// TODO Auto-generated constructor stub
this.s=s;
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
//封装通道内流
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
//为了防止名字冲突,使用时间戳
String newName=System.currentTimeMillis()+".java";
//封装文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter(newName));
//写入数据
String line=null;
while((line=br.readLine())!=null){//阻塞
bw.write(line);
bw.newLine();
bw.flush();
}
//给出反馈
BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
bwServer.write("文件上传成功");//阻塞
bwServer.newLine();
bwServer.flush();
//释放资源
bw.close();
s.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
package day26_Multi_Tcp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class UploadServer {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
//创建ServerSocket对象
ServerSocket ss = new ServerSocket(11111);
while(true){
//监听客户端
Socket s = ss.accept();//阻塞
new Thread(new UserThread(s)).start();
}
}
}