1. TCP concept:

      传输控制协议(Transmission Control Protocol, TCP)是一种面向连接(连接导向)的、可靠的、基于字节流的运输层(Transport layer)通信协议,由IETF的RFC 793说明(specified)。(from bing)

  2、TCP program:
      (1)、有明确的C/S之分,Server端用ServerSocket,Client端用Socke;

      (2)、Server端用accept()做監聽等待接收,一旦有鏈接連上的話,返回一個Socket對象作爲Client

      (3)、C/S都是用getInputStream/getOutputStream來讀取/寫入message

下面上代碼:

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket; public class ServerCode {
private static int port =4444;
static Socket cs;
public static void main(String[] args) throws IOException {
ServerSocket ss=new ServerSocket(port);
cs=ss.accept();
System.out.println("Accept :"+cs);
try{
BufferedReader br=new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintWriter pr=new PrintWriter(cs.getOutputStream(),true);
while(true){
String str=br.readLine();
if(str.equals("bye")){
break;
}
System.out.println("Client->Server:"+str);
pr.println("send back to client:"+str);
}
br.close();
pr.close();
}catch(Exception e){
System.out.println("error:"+e);
}finally{
System.out.println("close the Server socket and the io.");
cs.close();
ss.close();
} } }

ServerCode

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket; public class ClientCode {
public static void main(String[] args) throws IOException {
InetAddress add=InetAddress.getByName("localhost");
Socket cs=new Socket(add,4444);
System.out.println("socket = " + cs);
try{
BufferedReader br=new BufferedReader(new InputStreamReader(cs.getInputStream()));
PrintWriter pw=new PrintWriter(cs.getOutputStream(),true);
pw.println("Hello I'm qixi");
String str=br.readLine();
System.out.println(str);
pw.println("bye");
}catch(Exception e){
System.out.println(e);
}finally{
System.out.println("close the Client socket and the io.");
cs.close();
}
}
}

ClientCode

p.s.I:服务器是被动地监听一个客户端的连接请示然后给他们服务.服务器是由类ServerSocket来建立的.下面的程序建立了一个服务器端socket并把它绑定到80端口:

ServerSocket serverSocket = new ServerSocket(80, 5);

第一个参数是服务器要监听的端口.第二个参数是可选的.API文档中说明了这是一个监听时间,但是在传统的socket程序中第二个参数是监听深度.一个服务器可以同时接收多个连接请求,但是每次只能处理一个.监听堆是一个无回答的连接请求队列.如果省略了后面的一个参数,则默认值是50.
(from http://www.jb51.net/article/5551.htm)

Java study 2:The note of studying Socket which based TCP的更多相关文章

  1. Java study 1:The note of studying Socket which based UDP

    UDP concept: UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联) 参 ...

  2. java.net.SocketException: Software caused connection abort: socket write error

    用Java客户端程序访问Java Web服务器时出错: java.net.SocketException: Software caused connection abort: socket write ...

  3. [JAVA] java class 基本定义 Note

    java class 基本定义 Note 1 package abeen.note; 2 import java.util.*; 3 4 5 /* 6 java calss 基本 7 */ 8 pub ...

  4. Java从零开始学四十五(Socket编程基础)

    一.网络编程中两个主要的问题 一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后如何可靠高效的进行数据传输. 在TCP/IP协议中IP层主要负责网络主机的定位,数据传输的路由,由IP地址可 ...

  5. testNG java.net.SocketException: Software caused connection abort: socket write error

    执行用例报错,提示 java.net.SocketException: Software caused connection abort: socket write error java.net.So ...

  6. Caused by: java.net.SocketException: Software caused connection abort: socket write error

    1.错误描述 [ERROR:]2015-05-06 10:54:18,967 [异常拦截] ClientAbortException: java.net.SocketException: Softwa ...

  7. ClientAbortException: java.net.SocketException: Software caused connection abort: socket write erro

    1.错误描述 ClientAbortException: java.net.SocketException: Software caused connection abort: socket writ ...

  8. 【Java】NO.80.Note.1.Java.1.001-【Java 各种特性概念】

    1.0.0 Summary Tittle:[Java]NO.80.Note.1.Java.1.001-[Java 各种特性概念] Style:Java Series:Java Since:2018-0 ...

  9. 运用JAVA的concurrent.ExecutorService线程池实现socket的TCP和UDP连接

    运用JAVA的concurrent.ExecutorService线程池实现socket的TCP和UDP连接 最近在项目中可能要用到socket相关的东西来发送消息,所以初步研究了下socket的TC ...

随机推荐

  1. 【HDOJ】1829 A Bug's Life

    并查集变型.题意就是x与y是互斥的,下列是否数据是否可保证x-y是否均为互斥. #include <cstdio> #include <cstring> #define MAX ...

  2. [LeetCode#241]Different Ways to Add Parentheses

    Problem: Given a string of numbers and operators, return all possible results from computing all the ...

  3. screen space directional occlusion(SSDO) in Unity5

    也许是哪里出了问题..效果一般 16采样点 Gird . Random 博主近期渲染:最近用unity5弄的一些渲染 ---- by wolf96  http://blog.csdn.net/wolf ...

  4. Codevs 3304 水果姐逛水果街Ⅰ 线段树

    题目: http://codevs.cn/problem/3304/   时间限制: 2 s   空间限制: 256000 KB   题目等级 : 钻石 Diamond 题解       题目描述 D ...

  5. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  6. div样式调整小结 转载

    1.多个div使用会自动换行,应该使用float属性  left : 对象浮在左边  right : 对象浮在右边 例如:  float: left  和 float: right 是两个div左右排 ...

  7. centos7 mysql 5.6.30 默认配置文件

      默认配置 vim /etc/my.cnf.rpmsave mysql  Ver 14.14 Distrib 5.6.30, for linux-glibc2.5 (x86_64) using  E ...

  8. list去除重复

    1. [代码][Python]代码 简单去重      ? 1 2 3 4 5 l = [1,2,3,3] l = list(set(l))   >>>l >>>[ ...

  9. bzoj3907: 网格

    http://www.cnblogs.com/Tunix/p/4354348.html #include<cstdio> #include<cstring> #include& ...

  10. 跨服务器备注SQL数据库 分类: SQL Server 2015-03-05 08:52 227人阅读 评论(0) 收藏

    任务:把服务器1上的SQL数据库自动备份到服务器2上,命名格式=数据库名+年月日+小时. 说明: 服务器2=>192.168.0.22 数据库名=>Book 共享文件夹路径:192.168 ...