Java 基于TCP_IP协议的网络编程

来源:互联网 发布:网络阿里客服工作流程 编辑:程序博客网 时间:2024/04/29 05:57

网络编程TCP_IP Socket ServerSocket

TCP编程:Socket ServerSocket
下面是三个情况下的网络编程TCP_IP:
1.客户端发送内容给服务端,服务端将内容打印到控制台上。
2.客户端发送内容给服务端,服务端给予反馈。
3.从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。

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;//TCP编程例一:客户端给服务端发送信息,服务端输出信息到控制台//网络编程实际上就是Socket的编程public class TestTCP1 {    // 客户端    @Test    public void client() {        Socket socket = null;        OutputStream os = null;        try {            //1.创建一个Socket的对象,通过构造器指明服务端的ip地址以及其接受程序的端口号            socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);            //2.getOutputStream(),发送数据,方法返回OutputStream的对象            os = socket.getOutputStream();            //3.具体的输出过程            os.write("我是客户端~".getBytes());        } catch (IOException e) {            e.printStackTrace();        } finally {            //4.关闭相应的流            if (os != null) {                try {                    os.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (socket != null) {                try {                    socket.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    // 服务端    @Test    public void server() {        ServerSocket ss = null;        Socket s = null;        InputStream is = null;        try {            //1.创建一个ServerSocket的对象,通过构造器指明自身的对象            ss = new ServerSocket(9090);            //2.调用其accept()地方法,返回一个Socket的对象            s = ss.accept();            //3.调用Socket的getInputStream()方法,获取一个从客户端发送过来的输入流            is = s.getInputStream();            //4.对获取到的流进行的操作            byte[] b = new byte[20];            int len;            while ((len = is.read(b)) != -1) {                String str = new String(b, 0, len);                System.out.println(str);            }            System.out.println("收到来自于" + s.getInetAddress().getHostAddress());        } catch (IOException e) {            e.printStackTrace();        } finally {            //5.关闭相应的Socket、SocketServer的对象            if (is != null) {                try {                    is.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            if (is != null) {                try {                    s.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }            if (is != null) {                try {                    ss.close();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }}
import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;import org.junit.Test;//TCP编程例二:客户端给服务端发送信息,服务端将信息打印到控制台,同时发送“已收到信息”给服务端public class TestTCP2 {    // 客户端    @Test    public void client() {        Socket socket = null;        OutputStream os = null;        InputStream is = null;        try {            socket = new Socket("127.0.0.1", 8989);            os = socket.getOutputStream();            os.write("我是客户端".getBytes());            //shutdownOutput()显式地告诉服务端发送完毕            socket.shutdownOutput();            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);            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (is != null) {                try {                    is.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (os != null) {                try {                    os.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (socket != null) {                try {                    socket.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    // 服务端    @Test    public void server() {        ServerSocket ss = null;        Socket s = null;        InputStream is = null;        OutputStream os = null;        try {            ss = new ServerSocket(8989);            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.println(str);            }            os = s.getOutputStream();            os.write("我已收到你的信息".getBytes());        } catch (IOException e) {            e.printStackTrace();        } finally {            if (os != null) {                try {                    os.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (is != null) {                try {                    is.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (s != null) {                try {                    s.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (ss != null) {                try {                    ss.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}

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;

//TCP编程例三
//3.从客户端发送文件给服务端,服务端保存到本地。
//并返回“发送成功”给客户端。并关闭相应的连接
public class TestTCP3 {
@Test
public void client() {
// 1.创建Socket对象
Socket socket = null;
// 2.从本地获取一个文件发送服务器
OutputStream os = null;
FileInputStream fis = null;
// 3.接收来自服务器的信息
InputStream is = null;
try {
socket = new Socket(InetAddress.getByName(“127.0.0.1”), 8888);

        os = socket.getOutputStream();        fis = new FileInputStream(new File("1.jpg"));        byte[] b = new byte[1024];        int len;        while ((len = fis.read(b)) != -1) {            os.write(b, 0, len);        }        socket.shutdownOutput();        is = socket.getInputStream();        byte[] b1 = new byte[1024];        int len1;        while ((len = is.read(b1)) != -1) {            String str = new String(b1);            System.out.println(str);        }    } catch (IOException e) {        // TODO Auto-generated catch block        e.printStackTrace();    } finally {        // 4.关闭相应的流和Socket对象        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 (fis != null) {            try {                fis.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        if (socket != null) {            try {                socket.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }}@Testpublic void server() {    // 1.创建一个ServerSocket对象    ServerSocket ss = null;    // 2.调用accept()方法,返回一个Socket()的对象    Socket s = null;    // 3.将从客户端发送来的信息保存在本地    InputStream is = null;    FileOutputStream fos = null;    // 4.发送反馈信息    OutputStream os = null;    try {        ss = new ServerSocket(8888);        s = ss.accept();        is = s.getInputStream();        fos = new FileOutputStream(new File("4.jpg"));        byte[] b = new byte[1024];        int len;        while ((len = is.read(b)) != -1) {            fos.write(b, 0, len);        }        System.out.println("收到来自于" + s.getInetAddress().getHostName());        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 (fos != null) {            try {                fos.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();            }        }    }}

}

0 0
原创粉丝点击