Socket Programming in C

Slides:



Advertisements
Similar presentations
动态网站开发 【HTTP与网络基础】 李博杰
Advertisements

網路程式設計 (Network Programming)
第五章 網際網路 5-1 網際網路的歷史沿革 5-2 網際網路基本運作原理 5-3 連線媒介與連線上網 5-4 網際網路上的熱門應用
Rfc3315 Dynamic Host Configuration Protocol for IPv6 (DHCPv6) 組員: 蔡承翰 A 陳鈺璋 A 翁菘㠙 A 指導老師 吳俊興.
第 12 章 UDP 與 TCP.
马志强 软件学院501室 网络应用开发 马志强 软件学院501室
Chapter 12 UDP 與 TCP.
第2章 计算机网络的协议与体系结构 2.1 计算机网络体系结构的形成 2.2 协议与划分层次 2.3 计算机网络的原理体系结构
操作系统结构.
数据转发过程.
NetGuru 創新 網路通訊實驗教學解決方案 PART I TCP/IP通訊協定深入剖析/以NetGuru實作
TCP Session Hijack 郭军权
Socket.
计算机网络实验介绍 信息网络实验室 2017/9/13 04:55:22.
Lab312.
Group multicast fanOut Procedure
传输层是整个协议层次的核心,其任务是在源机器和目标机器之间提供可靠的、性价比合理的数据传输功能,并与当前所使用的物理网络完全独立
Advanced Sockets Programming
第 12 章 UDP 與 TCP.
TCP、UDP 通信实践 广州创龙电子科技有限公司 01 广州创龙电子科技有限公司
計中「多媒體與網路應用」短期訓練課程 FTP server 架設 (in Windows)
Operating System Concepts 作業系統原理 Chapter 3 行程觀念 (Process Concept)
基於OpenWSN之無線感測網路系統的實作
第3讲 网络安全协议基础 此为封面页,需列出课程编码、课程名称和课程开发室名称。
網路伺服器應用 Linux Server Andres, Wen-Yuan Liao
Socket Programming.
第五章 網際網路 5-1 網際網路的歷史沿革 5-2 網際網路基本運作原理 5-3 連線媒介與連線上網 5-4 網際網路上的熱門應用
Socket 基本觀念.
附錄 通訊協定堆疊.
Operating System Concepts 作業系統原理 CHAPTER 2 系統結構 (System Structures)
TCP/IP Protocol Suite TCP/IP協定 第二組 投影片製作by簡嘉宏 綦凱宏 林睿敏 滕孟哲.
Draft Amendment to STANDARD FOR Information Technology -Telecommunications and Information Exchange Between Systems - LAN/: R: Fast BSS.
Chapter 3 行程觀念 (Process Concept)
创建型设计模式.
在一定程度上 人类的思维产生于 简单个体之间的相互作用 ——Marvin Minsky.
第十讲 TCP协议 协议概述 报文段格式 差错控制 流控和拥塞控制 TCP连接管理 TCP性能问题 TCP软件设计 2018/12/7
第4章 OSI傳輸層.
中国科学技术大学计算机系 陈香兰(0512- ) Autumn 2010
第4讲 传输层之二 本讲目的: 本讲概述: Internet传输层的实现和实例 面向连接的传输: TCP TCP拥塞控制 拥塞控制原则
计算机网络(第 5 版) 第 5 章 传输层.
實驗目的 明瞭可靠傳輸層的基礎觀念 TCP協定下區段資料傳送的格式
第七讲 网际协议IP.
NS2 – TCP/IP Simulation How-Wei Wu.
Alcatel - Lucent SIGTRAN introduction 心得簡報
第2章 套接字网络编程基础 2.1 UNIX套接字网络编程接口的 产生与发展 2.2 套接字编程的基本概念 2.3 面向连接的套接字编程
校園網路架構介紹與資源利用 主講人:趙志宏 圖書資訊館網路通訊組.
第5讲 网络层 本讲目的: 概述: 理解网络层服务原理: 因特网的实现实例 网络层的服务 路由选择原理 分层的路由选择 IP协议
第 12 章 UDP 與 TCP 著作權所有 © 旗標出版股份有限公司.
第十三章 TCP/IP 與 Internet 網路連結技術
PubMed整合显示图书馆电子资源 医科院图书馆电子资源培训讲座.
第7章 传输层协议——TCP与UDP 任课教师 卢豫开.
Westmont College 网络互连 Part 4 (传输协议, UDP and TCP, 协议端口)
Web Server 王宏瑾.
Westmont College 网络应用软件 第一讲 (客户-服务器 概念, 协议端口的使用, 套接字API)
Advister: Quincy Wu Speaker: Chenglin Tsai Date:3/26
Wireshark DNS&HTTP封包分析
3.1 通訊協定 3.2 開放系統參考模式(OSI) 3.3 公眾數據網路 3.4 TCP/IP通訊協定
Speaker: Liu Yu-Jiun Date: 2009/4/29
Speaker : Chang Kai-Jia Date : 2010/04/26
面向非连接的 SOCKET编程 本节内容 视频提供:昆山爱达人信息技术有限公司 视频录制:yang 官网地址:
SOCKET( ).
2019/5/3 JAVA Socket(UDP).
Distance Vector vs Link State
Chapter 10 Mobile IP TCP/IP Protocol Suite
Mobile IPv4.
Distance Vector vs Link State Routing Protocols
助教:廖啟盛 JAVA Socket(UDP) 助教:廖啟盛
Internet课程设计 教师:陈 妍 朱海萍 西安交通大学计算机系
第7章 传输层协议——TCP与UDP 任课教师 卢豫开.
Presentation transcript:

Socket Programming in C Professor: Dr. Shu-Ching Chen TA: Yimin Yang

What is socket? An interface between application processes An Application Programming Interface (API) used for InterProcess Communications (IPC) Sockets bound to some IP and port number The socket library provides various system calls Examples: socket(), bind(), listen(), connect(), accept(),send(), recv(), close() – The application process can send/receive messages to/from another application process (local or remote)via a socket An interface through which processes can send /receive information Socket:兩個互相溝通的程序(process)之間的任一端點。此兩個process可同屬一電腦系統之內,或分屬於兩個不同的電腦系統透過網路來溝通。 使用目的:網路應用程式設計者不需處理網路下層(傳輸層、網路層)的工作,而專注於其本身(應用層)的程式設計。

Client-server paradigm initiates contact with server (“speaks first”) typically requests service from server, For Web, client is implemented in browser; Server: provides requested service to client typically requests service from server, e.g., Web server sends requested Web page. – The application process can send/receive messages to/from another application process (local or remote)via a socket An interface through which processes can send /receive information Socket:兩個互相溝通的程序(process)之間的任一端點。此兩個process可同屬一電腦系統之內,或分屬於兩個不同的電腦系統透過網路來溝通。 使用目的:網路應用程式設計者不需處理網路下層(傳輸層、網路層)的工作,而專注於其本身(應用層)的程式設計。

Types of socket (1) Two types of internet Sockets Stream sockets SOCK_STREAM Connection oriented Rely on TCP to provide reliable two-way connected communication Datagram sockets SOCK_DGRAM Rely on UDP Connection is unreliable Stream sockets use TCP (Transmission Control Protocol), which is a reliable, stream oriented protocol, and datagram sockets use UDP (Unix Datagram Protocol), which is unreliable and message oriented. UDP is commonly used for streaming audio and video. UDP is only concerned with speed.

Types of socket (2) Stream sockets – connection-oriented (TCP) Connection–based sockets communicate client-server: the server waits for a connection from the client Three-way handshake A:封包發起 當用戶端想要對伺服器端連線時,就必須要送出一個要求連線的封包,此時用戶端必須隨機取用一個大於 1024 以上的埠口來做為程式溝通的介面。然後在 TCP 的表頭當中,必須要帶有 SYN 的主動連線(SYN=1),並且記下發送出連線封包給伺服器端的序號 (Sequence number = 10001) 。 B:封包接收與確認封包傳送 當伺服器接到這個封包,並且確定要接收這個封包後,就會開始製作一個同時帶有 SYN=1, ACK=1 的封包, 其中那個 acknowledge 的號碼是要給 client 端確認用的,所以該數字會比(A 步驟)裡面的 Sequence 號碼多一號 (ack = 10001+1 = 10002), 那我們伺服器也必須要確認用戶端確實可以接收我們的封包才行,所以也會發送出一個 Sequence (seq=20001) 給用戶端,並且開始等待用戶端給我們伺服器端的回應喔! C:回送確認封包 當用戶端收到來自伺服器端的 ACK 數字後 (10002) 就能夠確認之前那個要求封包被正確的收受了, 接下來如果用戶端也同意與伺服器端建立連線時,就會再次的發送一個確認封包 (ACK=1) 給伺服器,亦即是 acknowledge = 20001+1 = 20002 囉。 D:取得最後確認 若一切都順利,在伺服器端收到帶有 ACK=1 且 ack=20002 序號的封包後,就能夠建立起這次的連線了。

Types of socket (3) Datagram sockets- connectionless socket (UDP) UDP user datagram protocol 不會運用確認機制來保證資料是否正確的被接收、不需要重傳遺失的資料、資料的接收可不必按順序進行、也不提供回傳機制來控制資料流的速度 A datagram socket provides a symmetric data exchange interface without requiring connection establishment. Each message carries the destination address. sendto(s, buf, buflen, flags, (struct sockaddr *) &to, tolen); The s, buf, buflen, and flags parameters are the same as in connection-oriented sockets. The to and tolen values indicate the address of the intended recipient of the message. A locally detected error condition, such as an unreachable network, causes a return of -1 and errno to be set to the error number. recvfrom(s, buf, buflen, flags, (struct sockaddr *) &from, &fromlen); To receive messages on a datagram socket, recvfrom(3SOCKET) is used. Before the call, fromlen is set to the size of the from buffer. On return, fromlen is set to the size of the address from which the datagram was received.

Primary Socket Calls (1) Socket() : Returns a file descriptor(socket ID) if successful, -1 otherwise. Arguments Domain: set to AF_INET Type: SOCK_STREAM SOCK_DGRAM SOCK_SEQPACKET Protocol: If it is set as zero, then socket will choose the correct protocol based on type. int socket(int domain, int type, int protocol); The domain argument specifies the address family used in the communications domain.  Protocol Family: PF_INET or PF_UNI The type argument can be: I SOCK STREAM: Establishes a virtual circuit for stream I SOCK DGRAM: Establishes a datagram for communication I SOCK SEQPACKET: Establishes a reliable, connection based, two way communication with maximum message size. (This is not available on most machines.) protocol is usually zero, so that type defines the connection within domain. The call returns a integer identifier called a handle

Primary Socket Calls (2) Bind() : Associate a socket id with an address to which other processes can connect. Arguments Sockfd: It is the socket id My_addr: a pointer to the address family dependent address structure Addrlen: It is the size of *my_addr int bind(int sockfd, struct sockaddr *my_addr, int addrlen); sockfd is the socket descriptor returned by socket() – my_addr is pointer to struct sockaddr that contains information about your IP address and port – addrlen is set to sizeof(struct sockaddr) – returns -1 on error – my_addr.sin_port = 0; //choose an unused port at random – my_addr.sin_addr.s_addr = INADDR_ANY; //use my IP adr

Primary Socket Calls (3) Listen() : Return 0 on success, or –1 if failure Arguments Sockfd: It is socket id created by socket() Backlog : It is used to constraint the number of connection int listen(int sockfd, int backlog); listen for connections on a socket SYNOPSIS Where size it the number of pending connection requests allowed (typically limited by Unix kernels to 5).

Primary Socket Calls (4) Connect() : connect to a remote host Arguments Sockfd: It is the socket descriptor returned by socket() serv_addr : It is a pointer to to struct sockaddr that contains information on destination IP address and port Addrlen: It is set to sizeof(struct sockaddr) int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); Specifies the destination to form a connection with (addrPtr), and returns a 0 if successful, -1 otherwise.

Primary Socket Calls (5) Accept() : gets the pending connection on the port you are listen()ing on. Arguments Sockfd: It is the same socket id used by listen() Addr: It is a pointer to a local struct sockaddr which stores the information about incoming connection Addrlen: It is set to sizeof(struct sockaddr_in) int accept(int sockfd, struct sockaddr *addr, int *addrlen); if lenPtr or addrPtr equal zero, no address structure is returned. lenPtr is the maximum size of address structure that can be called, returns the actual value. Waits for an incoming request, and when received creates a socket for it.

Primary Socket Calls (6) Send() : Send a message. Returns the number of bytes sent or -1 if failure. Arguments Sockfd: It is the same socket id used by socket() or accept() msg: It is the pointer to the data you want to send Len: data length is sizeof(msg) Flags : It is set to be zero int send(int sockfd, const void *msg, int len, int flags); flag is either I 0: default I MSG OOB: Out-of-band high priority communication

Primary Socket Calls (7) recv() : Receive up to len bytes in buf. Returns the number of bytes received or -1 on failure. Arguments Sockfd: It is the socket descriptor to read from buf: It is the buffer to read the information info Len: It is the maximum length of the buffer Flags : It is set to be zero int recv(int sockfd, void *buf, int len, unsigned int flags); returns the number of bytes actually read into the buffer or -1 on error returns 0, the remote side has closed connection on you flags can be either I 0: default I MSG OOB: out-of-bound message I MSG PEEK: look at message without removing

Primary Socket Calls (8) shutdown() : disable sending or receiving based on the value how. Arguments Sockfd How Set it to 0 will disable receiving Set it to 1 will disable sending Set it to 2 will disable both sending and receiving int shutdown(int sockfd, int how);

Primary Socket Calls (9) Close() : Close connection corresponding to the socket descriptor and frees the socket descriptor. int close(int sockfd)

example – server (1) read() is equivalent to recv() with a flags parameter of 0. Other values for the flags parameter change the behaviour of recv(). Similarly, write() is equivalent to send() with flags == 0.

example – server (2) bzero function will be used to set all the socket structures with null values void bzero(void *s, int nbyte); This function does not return anything. Parameters: s: specifies string which has to be filled with null bytes.This will be a point to socket structure variable nbyte: specifies the number of bytes to be filled with null values. This will be the size of the socket structure.

example – Client (1) The bcopy function copies nbyte bytes from string s1 to the string s2. Overlapping strings are handled correctly. void bcopy(const void *s1, void *s2, int nbyte); This function does not return anything. Parameters: s1: specifies the source string. s2: specifies the destination string. nbyte: specifies the number of bytes to be copied.

example – client (2)

Reference http://home.iitk.ac.in/~chebrolu/ee673-f06/sockets.pdf ftp://ftp.sas.com/techsup/download/SASC/share5958- 59/S5958v2.pdf http://www.slideshare.net/jignesh/socket-programming-tutorial http://www.rites.uic.edu/~solworth/sockets.pdf Beej's Guide to Network Programming Using Internet Sockets http://beej.us/guide/bgnet/output/html/multipage/index.html Sockets Tutorial http://www.linuxhowtos.org/C_C++/socket.htm