TCP入门简单例子
TCP的简单例子
TCP最简单聊天室
客户端
-
连接服务器 Socket
-
发送消息
package TCP; import java.io.IOException; import java.io.OutputStream; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner; //客户端 public class TcpClientZx01 { public static void main(String[] args) { Socket socket=null; OutputStream os=null; Scanner sc=null; //1.要知道服务端的地址 try { InetAddress serverIp= InetAddress.getByName("127.0.0.1"); int port=9090; //2.建立一个socket连接 socket=new Socket(serverIp,port); os= socket.getOutputStream(); sc=new Scanner(System.in); System.out.println("进入聊天室和邓聊天"); System.out.println("1为发送"); String e=""; while(!e.equals("1")){ e=sc.next(); os.write(e.getBytes()); } } catch (Exception e) { e.printStackTrace(); }finally{ try { if(os!=null){ os.close(); } if(socket!=null){ socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
服务端
-
建立服务的端口 ServerSocket
-
等待用户的连接 accept(等待监听)
-
接受用户的消息
public class TcpServerZx01 { //服务端 public static void main(String[] args) { ServerSocket serverSocket=null; Socket accept=null; InputStream is=null; ByteArrayOutputStream baos=null; try { //1.我得有一个地址 serverSocket=new ServerSocket(9090); while(true) { //监听客户端发过来的数据 accept=serverSocket.accept(); //2.读取客户端的数据 is=accept.getInputStream(); //管道流 baos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len = 0; while ((len = is.read(buf)) != -1) { baos.write(buf, 0, len); } System.out.printf(baos.toString()); } } catch (IOException e) { e.printStackTrace(); }finally{ if(baos!=null) { try { baos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(is!=null) { try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(accept!=null) { try { accept.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(serverSocket!=null) { try { serverSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
TCP最简单文件上传
文件上传:其实就是IO流,把资源转换成流,读取给出
客户端
public class FileTop {
public static void main(String[] args) throws UnknownHostException, IOException {
//1.创建一个Socket连接
Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),9999);
//2.创建一个输出流
OutputStream os=socket.getOutputStream();
//3.读取文件
FileInputStream fis = new FileInputStream(new File("img_7.jpg"));
//4.写出文件
byte[] buffer=new byte[1024];//这些都是固定代码,如果明确文件大小,可以不声明1024,避免资源浪费
int len;
while((len=fis.read(buffer))!=-1) {
os.write(buffer,0,len);
}
//通知服务器,我已经传输完了
socket.shutdownOutput();
//确定服务器接受完毕才能断开连接
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buffer2=new byte[1024];
int len2;
while((len2=is.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
//关闭资源,规则都遵循先写后关
baos.close();
is.close();
fis.close();
os.close();
socket.close();
}
}
服务端
public class FileDown {
public static void main(String[] args) throws IOException {
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9999);
//2.监听客户端的连接
Socket socket=serverSocket.accept();//监听,会一直等待客户连接
//3.获取输入流
InputStream is=socket.getInputStream();
//4.文件输出
FileOutputStream fos=new FileOutputStream(new File("reactiv.jpg"));
byte[] buffer=new byte[1024];
int len;
while((len=is.read(buffer))!=-1) {
fos.write(buffer,0,len);
}
//通知客户端接收完毕了
OutputStream os=socket.getOutputStream();
os.write("我接受完毕了".getBytes());
//5.关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}

浙公网安备 33010602011771号