package lianxi1;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 org.junit.Test;public class TestTCP1 {// 客戶端 @Testpublic void client() {Socket s = null;InputStream is = null;OutputStream os = null;try {s = new Socket(InetAddress.getByName("127.0.0.1"), 9090);//s = new Socket(InetAddress.getByName("221.192.74.152"), 9090); //對方主機的IP地址os = s.getOutputStream();String str1 = "我是客戶端,請接收";os.write(str1.getBytes());s.shutdownOutput();is = s.getInputStream();byte[] b = new byte[20];int len;while((len=is.read(b))!=-1){String str = new String(b,0,len);System.out.print(str);}} catch (Exception e) {// TODO: handle exception }finally{if (is != null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}if (os != null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}if (s != null) {try {s.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}}}// 服務器 @Testpublic void server() {ServerSocket ss = null;Socket s = null;InputStream is = null;OutputStream os = null;try {ss = new ServerSocket(9090);s = ss.accept();is = s.getInputStream();byte[] b = new byte[20];int len;while ((len = is.read(b)) != -1) {String str = new String(b, 0, len);System.out.print(str);}os = s.getOutputStream();os.write("已收到客戶端信息".getBytes());} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}finally{if (os != null) {try {os.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 (s != null) {try {s.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}if (ss != null) {try {ss.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}}} }
package lianxi1;import java.io.File; import java.io.FileInputStream; 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 org.junit.Test;public class TestTCP2 {// 客戶端 @Testpublic void client() {Socket s = null;InputStream is = null;OutputStream os = null;FileInputStream fis = null;try {s = new Socket(InetAddress.getByName("127.0.0.1"), 9090);//s = new Socket(InetAddress.getByName("221.192.74.152"), 9090); //對方主機的IP地址OutputStream fos = s.getOutputStream(); fis = new FileInputStream(new File("11.jpg"));byte[] b = new byte[1024];int len;while((len=fis.read(b))!=-1){fos.write(b, 0, len);}s.shutdownOutput(); //因為服務器端監聽端口,所以只在客戶端寫 is = s.getInputStream();byte[] b2 = new byte[10];int len2;while((len2=is.read(b2))!=-1){String str = new String(b2,0,len2);System.out.println(str);}} catch (Exception e) {// TODO: handle exception }finally{if (fis != null) {try {fis.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}if (os != null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}if (is != null) {try {os.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}if (s != null) {try {s.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}}}// 服務器 @Testpublic void server() {ServerSocket ss = null;Socket s = null;InputStream is = null;OutputStream os = null;try {ss = new ServerSocket(9090);s = ss.accept();FileOutputStream fos = new FileOutputStream("12.jpg");is = s.getInputStream();byte[] b = new byte[1024];int len;while ((len = is.read(b)) != -1) {fos.write(b, 0, len);}os = s.getOutputStream();os.write("已收到客戶端信息".getBytes());} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}finally{if (os != null) {try {os.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 (s != null) {try {s.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}if (ss != null) {try {ss.close();} catch (IOException e) {// TODO Auto-generated catch block e.printStackTrace();}}}} }