TCP Socket通讯模型

TCP Socket通讯模型

 

TCP Socket通讯模型


应用举例:

TestServer.java

TestCilent.java

 

TestServer.java

/*	范例名称:简单的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);			
		}
	}
}

 

F:\>cd java

F:\java>cd socket

F:\java\socket>javac TestServer.java

F:\java\socket>java TestServer


 

TestClient.java

/*	范例名称:简单的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();
		}
	}
}

 

F:\>cd java

F:\java>cd socket

F:\java\socket>javac TestClient.java

F:\java\socket>java TestClient
Hello,/127.0.0.1port#50883  bye-bye!

F:\java\socket>java TestClient
Hello,/127.0.0.1port#50890  bye-bye!

F:\java\socket>java TestClient
Hello,/127.0.0.1port#50892  bye-bye!

F:\java\socket>java TestClient
Hello,/127.0.0.1port#50893  bye-bye!

F:\java\socket>

 

 测试

TestSockServer.java

TestSockClient.java

 

TestSockServer.java

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);
	  while(true) {
		  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);
			  //打印client的地址和端口
			  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();}
  }
}

 

TestSockClient.java

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

 

F:\java\socket>javac TestSockServer.java

F:\java\socket>java TestSockServer
hey
from: /127.0.0.1
Port: 51812
hey
from: /127.0.0.1
Port: 51814
hey
from: /127.0.0.1
Port: 51815
hey
from: /127.0.0.1
Port: 51817

 

F:\java\socket>javac TestSockClient.java

F:\java\socket>java TestSockClient
hi,hello

F:\java\socket>java TestSockClient
hi,hello

F:\java\socket>java TestSockClient
hi,hello

F:\java\socket>java TestSockClient
hi,hello

F:\java\socket>

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值