tcp/ip網絡編程結課論文,TCP/IP網絡編程

 2023-11-07 阅读 20 评论 0

摘要: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
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();}}}}
}

轉載于:https://www.cnblogs.com/yjtm53/p/4167721.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/168047.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息