码迷,mamicode.com
首页 > 其他好文 > 详细

J2SE之网络编程

时间:2015-09-05 23:48:12      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

网络基础概念

技术分享

网络通信协议及接口

技术分享

通信协议分层的思想

技术分享

参考模型

技术分享

数据封装

技术分享

数据拆封

技术分享

IP协议

技术分享

TCP协议和UDP协议

技术分享

Socket

技术分享

技术分享
import java.net.*;
import java.io.*;

public class TCPServer
{
    public static void main(String[] args)throws Exception{
        
        ServerSocket ss = new ServerSocket(6666);
        while (true)
        {
            Socket s = ss.accept();
            DataInputStream dis = new DataInputStream(s.getInputStream());
            System.out.println(dis.readUTF());
            dis.close();
            s.close();
        }
        
    }
}
TCPServer

 

技术分享
import java.net.*;
import java.io.*;

public class TCPClient
{
    public static void main(String[] args) throws Exception{
        Socket s = new Socket("127.0.0.1",6666);
        OutputStream os = s.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        Thread.sleep(3000);
        dos.writeUTF("hello server");
        dos.flush();
        dos.close();
        s.close();
    }
}
TCPClient

 

TCP Socket 通信模型

技术分享

举例

技术分享

技术分享
/*    范例名称:简单的client/server程序
 *     源文件名称:TestClient.java/TestServer.java
 *    要  点:
 *        1. Java Socket编程步骤
 *        2. Socket/ServerSocket类用法
 *        3. 通过Socket对象可以获取通信对方Socket的信息
 */
import java.net.*;
import java.io.*;

public class TestServer {
    public static void main(String args[]) {
        try {        
            ServerSocket s = new ServerSocket(8888);
            while (true) {
                Socket s1 = s.accept();
                OutputStream os = s1.getOutputStream();
                DataOutputStream dos = new DataOutputStream(os);
                dos.writeUTF("Hello," + s1.getInetAddress() + 
                        "port#" +s1.getPort() + "  bye-bye!");
                dos.close();
                s1.close();
            }
        }catch (IOException e) {
            e.printStackTrace();
            System.out.println("程序运行出错:" + e);            
        }
    }
}
TestServer

 

技术分享
/*    范例名称:简单的client/server程序
 *     源文件名称:TestClient.java/TestServer.java
 *    要  点:
 *        1. Java Socket编程步骤
 *        2. Socket/ServerSocket类用法
 *        3. 通过Socket对象可以获取通信对方Socket的信息
 */

import java.net.*;
import java.io.*;

public class TestClient {
    public static void main(String args[]) {
        try {
            Socket s1 = new Socket("127.0.0.1", 8888);
            InputStream is = s1.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            System.out.println(dis.readUTF());
            dis.close();
            s1.close();
        } catch (ConnectException connExc) {
            connExc.printStackTrace();
            System.err.println("服务器连接失败!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
TestClient

 

技术分享
import java.io.*; 
import java.net.*;
public class TestSockServer {
  public static void main(String[] args) {
    InputStream in = null; 
    OutputStream out = null;
    try {
      ServerSocket ss = new ServerSocket(5888);
      Socket socket = ss.accept();
      in = socket.getInputStream(); 
      out = socket.getOutputStream();
      DataOutputStream dos = new DataOutputStream(out);
      DataInputStream dis = new DataInputStream(in);
      String s = null;
      if((s=dis.readUTF())!=null) {
          System.out.println(s);
          System.out.println("from: "+socket.getInetAddress());
          System.out.println("Port: "+socket.getPort());
        }
      dos.writeUTF("hi,hello"); 
      dis.close();
      dos.close();
      socket.close();
    } catch (IOException e) {e.printStackTrace();}
  }
}
TestSockServer
技术分享
import java.net.*;
import java.io.*;
public class TestSockClient {
  public static void main(String[] args) {
    InputStream is = null; OutputStream os = null;
    try {
      Socket socket = new Socket("localhost",5888);
      is = socket.getInputStream();
      os = socket.getOutputStream();
      DataInputStream dis = new DataInputStream(is);
      DataOutputStream dos = new DataOutputStream(os);
      dos.writeUTF("hey"); 
      String s = null;
      if((s=dis.readUTF())!=null);
          System.out.println(s); 
      dos.close();
      dis.close();
      socket.close();
    } catch (UnknownHostException e) {
       e.printStackTrace();
    } catch (IOException e) {e.printStackTrace();}
  }
}
TestSockClient

 

技术分享
import java.io.*;
import java.net.*;
import java.applet.Applet;
public class talkserver
{
    public static void main(String args[])
    {
        try
        {
            ServerSocket server = null;
            try
            {
                server = new ServerSocket(4700);
            }catch(Exception e)
            {
                System.out.println("can not listen to:" + e);
            }
            Socket socket = null;
            try
            {
                socket = server.accept();
            }catch(Exception e)
            {
                System.out.println("Error:" + e);
            }
            String line;
            BufferedReader is = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
            PrintWriter os = new PrintWriter(socket.getOutputStream());
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Client:" + is.readLine());
            line = sin.readLine();
            while (!line.equals("bye"))    
            {
                os.println(line);
                os.flush();
                System.out.println("Server:" + line);
                System.out.println("Client:" + is.readLine());
                line = sin.readLine();
            }

            is.close();
            os.close();
            socket.close();
            server.close();
        }catch(Exception e)
        {
            System.out.println("Error" + e);
        }
    }
}
            
talkserver
技术分享
import java.io.*;
import java.net.*;
public class talkclient
{
    public static void main(String args[])
    {
        try
        {
            Socket socket = new Socket("127.0.0.1",4700);
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter os = new PrintWriter(socket.getOutputStream());
            BufferedReader is = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));
            String readline;
            readline = sin.readLine();
            while (!readline.equals("bye"))    
            {
                os.println(readline);
                os.flush();
                System.out.println("Client:" + readline);
                System.out.println("Server:" + is.readLine());
                readline = sin.readLine();
            }
            os.close();
            is.close();
            socket.close();
        }catch(Exception e)
        {
            System.out.println("Error" + e);
        }
    }
}
            
talkclient

 

J2SE之网络编程

标签:

原文地址:http://www.cnblogs.com/gimin/p/4784186.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!