下面的demo的目的是通过代码来快速的了解tcp编程的步骤
1首先呢,对InetAddress类简单了解其作用
public static void main(String[] args) throws Exception {
InetAddress inet=InetAddress.getByName("www.baidu.com"); System.out.println(inet);//百度地址 System.out.println(inet.getHostName());//ip地址对应的域名 System.out.println(inet.getHostAddress());//获取IP地址 InetAddress inet1=InetAddress.getLocalHost();//本地地址 System.out.println(inet1);//获取本机的地址ip }下面的demo的目的是:客户端给服务端发送消息,然后服务端打印到控制台上
package com.henu.liulei;
import java.io.IOException;
import java.io.InputStream;import java.io.OutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.UnknownHostException;import org.junit.Test;
public class TestTcp1 {
public static void main(String[] args) { }@Testpublic void client() { Socket socket=null; OutputStream os=null; try { socket=new Socket(InetAddress.getByName("127.0.0.1"),9090); os=socket.getOutputStream(); os.write("我是客户端".getBytes()); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if(os!=null) { os.close(); } if(socket!=null) { socket.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}@Testpublic void server() { try { ServerSocket socket=new ServerSocket(9090); Socket socket1=socket.accept(); InputStream is=socket1.getInputStream(); byte[]b=new byte[20]; int len; while((len=is.read(b))!=-1) { String str=new String(b,0,len); System.out.println(str); } is.close(); socket1.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}}下面的demo的目的是客户端给服务端发送消息,然后服务端打印到控制台上,同时给客户端反馈消息
package com.henu.liulei;
import java.io.IOException;
import java.io.InputStream;import java.io.OutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.UnknownHostException;import org.junit.Test;
public class TestTcp2 {@Test
public void client() { Socket socket=null; OutputStream os=null; try { socket=new Socket(InetAddress.getByName("127.0.0.1"),9090); os=socket.getOutputStream(); os.write("我是客户端".getBytes()); socket.shutdownOutput();//执行此方法告诉服务端表示已经发送完了 InputStream is=socket.getInputStream(); byte[]b=new byte[20]; int len; while((len=is.read(b))!=-1) { String str=new String(b,0,len); System.out.println(str); } is.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if(os!=null) { os.close(); } if(socket!=null) { socket.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}@Testpublic void server() { try { ServerSocket socket=new ServerSocket(9090); Socket socket1=socket.accept(); InputStream is=socket1.getInputStream(); byte[]b=new byte[20]; int len; while((len=is.read(b))!=-1) { String str=new String(b,0,len); System.out.println(str); } OutputStream os=socket1.getOutputStream(); os.write("收到".getBytes()); os.close(); is.close(); socket1.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}}最后一个demo的目的是从客户端发送给服务端,服务端接受并保存在本地,然后发消息给客户端表示接收到
package com.henu.liulei;
import java.io.File;
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.net.UnknownHostException;import org.junit.Test;
public class TestTcp3 { @Test public void client() { try { //1:创建socket对象, Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),9090); socket.setSoTimeout(10000); //从本地获取一个文件,发送给服务端 OutputStream os=socket.getOutputStream(); FileInputStream fis=new FileInputStream(new File("C:/aaaaa.jpg")); byte []b=new byte[1024]; int len; while((len=fis.read(b))!=-1) { os.write(b,0,len); } socket.shutdownOutput(); //接受来自服务端的信息 InputStream is= socket.getInputStream(); byte []b1=new byte[1024]; int len1; while((len1 = is.read(b1))!=-1) { String str=new String(b1,0 , len1); System.out.println(str); } //关闭相应的流 is.close(); fis.close(); os.close(); socket.close(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void server() { try { //1创建一个serversocket对象 ServerSocket ss=new ServerSocket(9090); //2:调用accept方法,返回一个socket对象 while(true) { Socket s=ss.accept(); //把接受到的信息保存在本地 InputStream is=s.getInputStream(); FileOutputStream fos=new FileOutputStream(new File("C:/bbbbb.jpg")); byte []b=new byte[1024]; int len ; while((len=is.read(b))!=-1) { fos.write(b, 0, len); } //发送“接收成功”的消息反馈给客户端 OutputStream os=s.getOutputStream(); os.write("你发送成功,已经接受".getBytes()); //关闭流 os.close(); fos.close(); is.close(); s.close(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}