1,入门例子
package javaBase.net.tcp;
import java.io.*;
import java.net.*;
/*
* 客户端client对应的对象是socket,
* 在该对象建立时就能指定主机,tcp是面向连接的,所以在建立Socket服务时就要有服务端存在
*/
class TcpClient {
public static void main(String[] args) throws Exception {
//1,创建客户端socket服务,并指定服务器端主机和端口
Socket s = new Socket("127.0.0.1",10003);
//2,为了发送数据,拿到socket中的输出流并写数据
OutputStream out = s.getOutputStream();
out.write("tcp client send....".getBytes());
//3,关闭socket
s.close();
}
}
/*
* 服务端serversocket 自己不创建socket,从客户端获取其socket
*/
public class TcpServer {
public static void main(String[] args) throws Exception{
//1,创建服务端的socket服务,Serversocket();并监听一个端口
ServerSocket ss = new ServerSocket(10003);
//2,获取客户端传来的对象,accept(),阻塞方法,等待
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();//获取客户端ip
//3,用客户端对象的读取流获取客户端的数据
InputStream in = s.getInputStream();
byte [] buf = new byte[1024];
int len = in.read(buf);
String data = new String(buf,0,len);
System.out.println(ip+": "+data );
//4,关闭客户端
s.close();
// ss.close();//服务器一般都不关
}
}
一次交互,客户端发送,服务端接收,再发送,客户端接收
package javaBase.net.tcp;
import java.io.*;
import java.net.*;
class TcpClient2 {
public static void main(String[] args) throws Exception {
Socket s = new Socket("127.0.0.1",10005);
OutputStream out = s.getOutputStream();
out.write("服务器,你好,我是客户端".getBytes());
//读取服务端发的数据
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);//阻塞方法,等待服务端数据
System.out.println("服务器说:"+new String(buf,0,len));
s.close();
}
}
public class TcpServer2 {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10005);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println("客户端,你的IP是:"+ip);
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = in.read(buf);
System.out.println("你刚才说:"+new String(buf,0,len)+"我睡4秒钟");
Thread.sleep(4000);
OutputStream out = s.getOutputStream();
out.write("hello,我说这句话你听到没".getBytes());
s.close();
}
}
服务端开启服务:将客户端的每一行字符转成大写返回
package javaBase.net.tcp;
import java.io.*;
import java.net.*;
class TcpClient3 {
public static void main(String[] args) throws Exception {
Socket s = new Socket("127.0.0.1",10006);
//键盘输入
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
//网络输出
PrintWriter pw =
new PrintWriter(s.getOutputStream(),true);
//网络输入
BufferedReader bufIn =
new BufferedReader(new InputStreamReader(s.getInputStream() ));
//循环获取键盘录入
String line = null;
while((line = in.readLine())!=null){
if("over".equals(line))
break;
pw.println(line);//PrintWriter设为true,这里自动flush(),println()带回车符,write()不带
//获取服务端返回的大写数据
String str = bufIn.readLine();
System.out.println("server:"+str);
}
in.close();//键盘录入不属于socket,自己关
s.close();
}
}
public class TcpServer3 {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10006);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"...connected");
//读取客户端流
BufferedReader in =
new BufferedReader(new InputStreamReader(s.getInputStream()));
//返回给客户端流
PrintWriter pw =
new PrintWriter(s.getOutputStream(),true);
//读取客户端数据
String line = null;
while((line = in.readLine())!=null){
pw.println(line.toUpperCase());
}
s.close();
// ss.close();
}
}
多客户端并发上传图片到服务器端
package javaBase.net.tcp;
import java.io.*;
import java.net.*;
/*
* 客户端上传图片到服务器端
*/
class PicClient {
public static void main(String[] args) throws Exception{
Socket s= new Socket("127.0.0.1",10007);
FileInputStream fin = new FileInputStream("man.jpg");
OutputStream out = s.getOutputStream();
//读写
byte[] buf = new byte[1024];
int len = 0;
while((len = fin.read(buf))!=-1){
out.write(buf,0,len);
}
s.shutdownOutput();
//接收服务器端返回数据
InputStream in = s.getInputStream();
byte[] bufIn = new byte[1024];
int num = in.read(bufIn);
System.out.println(new String(bufIn,0,num));
fin.close();
s.close();
}
}
/**
* 服务器端并发上传图片
* @author xukunn
* 多线程
*/
class PicThread implements Runnable{
private Socket s;
public PicThread(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+"....connected");
int count =1;
File file = new File("man_server"+"("+count+")" +".jpg");
while(file.exists()){
file = new File("man_server"+"("+(count++)+")" +".jpg");
}
FileOutputStream fout = new FileOutputStream(file);
InputStream in = s.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = in.read(buf))!=-1){
fout.write(buf,0,len);
}
//回应客户端
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fout.close();
s.close();
} catch (Exception e) {
throw new RuntimeException("上传失败");
}
}
}
/*
* 这个服务端只能处理一个客户端的请求,另一个客户端只能等第一个客户端上传完成了,才会再次调用accept()方法
* 所以服务端最好将每一个客户端封装到一个单独的线程中
*/
public class PicServer {
public static void main(String[] args) throws Exception{
ServerSocket ss = new ServerSocket(10007);
while(true){
Socket s = ss.accept();
new Thread(new PicThread(s)).start();
}
}
}