服务器创建完毕后 创建客户端:
[mw_shl_code=java,true]public class TCPClient3 {
public static void main(String[] args) throws IOException {
//1创建服务器对象
Socket s = new Socket("127.0.0.1", 9898);
//2指定路径
FileInputStream fis = new FileInputStream("D:\\123.png");
//3读取服务器数据
OutputStream out = s.getOutputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = fis.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
//4告知书写完毕
s.shutdownOutput();
//5书写到服务器
InputStream in = s.getInputStream();
len = in.read(bytes);
System.out.println("服务器:" + new String(bytes, 0, len));