The document discusses socket programming and provides an overview of client-server applications using sockets. It describes how sockets allow for interprocess communication using the client-server model. The key steps for creating TCP and UDP client and server applications in both C and Java programming languages are outlined, including how to create sockets, bind sockets, connect sockets, send and receive data. Code examples of a TCP client and server application written in C are also provided.
This document discusses socket programming concepts in Java including server-client communication using sockets, the InetAddress class and its methods like getLocalHost(), getByName(), and getAllByName(), and the Socket and ServerSocket classes. It provides code examples to demonstrate how to use these classes and methods to establish connections between a client and server and exchange data over the network.
The document discusses keyboard and file access in Java using input streams, readers, and buffers. It then covers socket programming in Java to connect to other computers over a network. A client-server model is described where the client connects to the server, which listens for connections on a port. The server accepts the connection and a separate socket is used to communicate with that client.
The document discusses network programming with sockets. It outlines basic socket programming concepts like client-server models, TCP and UDP services, and BSD sockets. It then describes key socket functions for creation and setup, establishing connections, sending/receiving data, and tearing down connections.
Socket programming uses a client-server model where the client initiates contact with the server to request a service. It uses sockets to allow two processes to communicate by sending and receiving data through the socket. The socket API provides functions to create, bind, listen for, accept, and communicate over sockets. It defines sockets as endpoints for communication between processes running on the same or different devices on a network.
Network programming in java - PPT with Easy Programs and examples of Java InetAddress Class and java socket programming example.
Learn more @ http://java2all.com/technology/network-programming
This document provides an overview of socket programming in Java. It defines a socket as an endpoint for two-way communication between programs over a network. The key classes for socket programming in Java are Socket for clients and ServerSocket for servers. It describes how to establish connections between clients and servers using these classes, set up input and output streams, and properly close connections. TCP sockets provide reliable, ordered connections while UDP sockets are unreliable and unordered. Exceptions that can occur during network programming are also listed.
Application Layer and Socket Programmingelliando dias
The document discusses computer networking concepts related to the application layer and socket programming. It provides an overview of client-server applications and protocols, and describes how sockets allow applications to communicate over TCP and UDP. Specifically, it explains how servers can use sockets, binding, listening and accepting to establish TCP connections with clients. It also explains how clients can use sockets and connecting to communicate with servers. For UDP, it describes how servers can use sockets and binding to receive datagrams, while clients can use sockets and sending to communicate with servers.
This document discusses socket programming concepts in Java including server-client communication using sockets, the InetAddress class and its methods like getLocalHost(), getByName(), and getAllByName(), and the Socket and ServerSocket classes. It provides code examples to demonstrate how to use these classes and methods to establish connections between a client and server and exchange data over the network.
The document discusses keyboard and file access in Java using input streams, readers, and buffers. It then covers socket programming in Java to connect to other computers over a network. A client-server model is described where the client connects to the server, which listens for connections on a port. The server accepts the connection and a separate socket is used to communicate with that client.
The document discusses network programming with sockets. It outlines basic socket programming concepts like client-server models, TCP and UDP services, and BSD sockets. It then describes key socket functions for creation and setup, establishing connections, sending/receiving data, and tearing down connections.
Socket programming uses a client-server model where the client initiates contact with the server to request a service. It uses sockets to allow two processes to communicate by sending and receiving data through the socket. The socket API provides functions to create, bind, listen for, accept, and communicate over sockets. It defines sockets as endpoints for communication between processes running on the same or different devices on a network.
Network programming in java - PPT with Easy Programs and examples of Java InetAddress Class and java socket programming example.
Learn more @ http://java2all.com/technology/network-programming
This document provides an overview of socket programming in Java. It defines a socket as an endpoint for two-way communication between programs over a network. The key classes for socket programming in Java are Socket for clients and ServerSocket for servers. It describes how to establish connections between clients and servers using these classes, set up input and output streams, and properly close connections. TCP sockets provide reliable, ordered connections while UDP sockets are unreliable and unordered. Exceptions that can occur during network programming are also listed.
Application Layer and Socket Programmingelliando dias
The document discusses computer networking concepts related to the application layer and socket programming. It provides an overview of client-server applications and protocols, and describes how sockets allow applications to communicate over TCP and UDP. Specifically, it explains how servers can use sockets, binding, listening and accepting to establish TCP connections with clients. It also explains how clients can use sockets and connecting to communicate with servers. For UDP, it describes how servers can use sockets and binding to receive datagrams, while clients can use sockets and sending to communicate with servers.
This document provides an overview of Java sockets including how they allow for client-server communication over networks, the lifecycle of a socket server, and code examples for a socket server and clients. It discusses how sockets provide connection-oriented and connectionless services in Java using classes like ServerSocket and Socket. Diagrams depict the use cases and classes for a socket server that handles weather requests from multiple clients. Code for a WeatherSocketServer class and examples of client requests are also included.
This is all about socket programming in java using TCP and UDP socket and an example of simple Echo Server.
Also includes concepts of the socket, Socket class and methods and use of those.
This document provides an overview of socket programming in Java. It discusses how client-server applications use sockets to communicate over a network. Sockets are identified by an IP address and port number. The document explains TCP and UDP socket programming in Java. For TCP, it describes how the server creates a welcoming socket to accept client connections. For both TCP and UDP, it outlines the basic interactions between client and server sockets. The document concludes by noting that socket programming is easy in Java and real-time applications typically use threads to handle each socket.
The document discusses network programming and Java sockets. It introduces elements of client-server computing including networking basics like TCP, UDP and ports. It then covers Java sockets, explaining how to implement both a server and client using Java sockets. Code examples are provided of a simple server and client. The conclusion emphasizes that Java makes socket programming easier than other languages like C.
Overview of socket API.
A network socket is an interface for an application to connect to a host‘s network stack (part of the OS). Sockets provide an abstraction of the network. Applications use the transport services available on the socket interface for communication with a peer application.
Transport protocol such as TCP, UDP or SCTP offer different levels of service. TCP for example provides reliable stream-oriented transport service while UDP more reflects the best-effort kind of service provided by the underlying IP protocol layer.
Sockets are bound to a transport address. For TCP/IP sockets this means an IP address and port number.
Sockets provide only very basic data transport services without any higher level functions like parameter marshaling or serialization. These functions have to be performed by the applications. These functions are often hidden in middleware platforms like distributed object technologies (CORBA, RMI, DCOM) or web service frameworks (WCF, JAX-WS).
Multicast sockets make the IP multicast capability accessible to applications. Multicast sockets are often used for streaming services where multiple applications are recipients of the same data packets.
Sockets allow for two-way communication between hosts in a network. There are two types of sockets: server sockets and client sockets. Server sockets wait for connection requests from clients, while client sockets are used to send and receive data from servers. Sockets provide input and output streams for transmission of data between endpoints.
Introduction
This Tutorial is On Socket Programming In C Language for Linux. Instructions Give Below will only work On Linux System not in windows.
Socket API In windows is called Winsock and there we can discuss about that in another tutorial.
What is Socket?
Sockets are a method for communication between a client program and a server program in a network.
A socket is defined as "the endpoint in a connection." Sockets are created and used with a set of programming requests or "function calls" sometimes called the sockets application programming interface (API).
The most common sockets API is the Berkeley UNIX C interface for sockets.
Sockets can also be used for communication between processes within the same computer.
Socket programming in Java allows applications to communicate over the internet. Sockets are endpoints for communication that are identified by an IP address and port number. A socket connection is established between a client and server socket. The server creates a welcoming socket to accept client connection requests, then a separate connection socket to communicate with that client. Data can be sent bidirectionally over the connected sockets as input/output streams. UDP uses datagram sockets without a connection, requiring the explicit destination address on each message.
The document discusses sockets and the client-server model for interprocess communication. It describes what sockets are, the different types of sockets (STREAM, DATAGRAM, RAW), and how they are used for reliable and unreliable communication between processes. It provides code examples for creating, connecting, sending, receiving and closing sockets in both client and server applications using TCP and UDP. Key system calls for sockets like socket(), bind(), listen(), accept(), connect(), send(), recv(), close() are also explained.
This document discusses client-server programming and socket programming. It begins by explaining the client-server model, where the client initiates contact with the server to request a service. It then covers application layer programming using sockets as the interface. Sockets provide an abstraction for network communication, representing an endpoint. The document outlines functions for creating, binding, listening for connections, accepting connections, and sending/receiving data with sockets. It also discusses related topics like network byte order, utility functions, and address data structures.
The presentation given at MSBTE sponsored content updating program on 'Advanced Java Programming' for Diploma Engineering teachers of Maharashtra. Venue: Guru Gobind Singh Polytechnic, Nashik
Date: 22/12/2010
Session: Java Network Programming
The InetAddress class in Java represents IP addresses and allows conversion between host names and IP addresses. It has subclasses for IPv4 (Inet4Address) and IPv6 (Inet6Address) addresses. InetAddress objects store both the raw IP address and associated host name. Methods are provided to look up addresses by name, check address properties like scope, and test address reachability. Caching is used to improve performance of name lookups.
This document discusses Java networking and client/server communication. A client machine makes requests to a server machine over a network using protocols like TCP and UDP. TCP provides reliable data transmission while UDP sends independent data packets. Port numbers map incoming data to running processes. Sockets provide an interface for programming networks, with ServerSocket and Socket classes in Java. A server program listens on a port for client connections and exchanges data through input/output streams. Servlets extend web server functionality by executing Java programs in response to client requests.
This document discusses socket programming in Java. It begins by defining what a socket is - the combination of an IP address and port number used to uniquely identify an endpoint in a network connection. It then covers the basics of client-server socket programming using both TCP and UDP, including creating and using sockets, streams, and datagrams. Example code is provided for both TCP and UDP client and server implementations in Java using sockets to send and receive data. The document concludes with references for more information on socket programming.
The document describes 8 programs related to networking in Java. Program 1 obtains the IP address of a website. Program 2 connects to the home page of yahoo.com and displays the HTML. Program 3 parses a URL into its components. Program 4 translates between IP addresses and host names. Program 5 prints all IP addresses of a domain. Program 6 obtains the local machine's IP address. Program 7 finds the host name given an IP address. Program 8 downloads a file from the web and saves or displays it. The last part describes a client-server program with a server that echoes back a string sent by the client.
Socket programming allows processes to communicate over a network. Key aspects include:
- Demultiplexing packets into processes using port numbers
- Converting between host and network byte order
- Sockets representing an endpoint for communication and identified by address/port pairs
- Functions for binding addresses, listening for connections, accepting connections, and sending/receiving data over established sockets.
Socket programming allows applications on networked computers to communicate reliably using TCP or unreliably using UDP. A socket represents an open connection between two endpoints and has methods to get input/output streams, the remote host/port, and local port. A client socket connects to a server, while a server socket listens on a port and accepts connections from clients.
The document discusses network programming in Java using the java.net package. It covers using TCP and UDP for network communication. TCP provides reliable, ordered streams between hosts using sockets, while UDP provides simpler datagram transmission that is unreliable but can be broadcast. The InetAddress class represents IP addresses, and sockets are used for both TCP clients/servers and UDP communication.
The overview on Socket Programming that will lead you how to program with a short example of programming which is in slide. With the help of this you can understand about network programming.
The document discusses socket programming and provides code examples for implementing client-server applications in C. It introduces sockets and the client-server model, and describes how to create TCP and UDP sockets. It then shows the steps to program a TCP client and server in C, including creating/connecting sockets, sending/receiving data, and closing connections. Key system calls like socket(), bind(), listen(), accept(), send(), and recv() are explained.
This document discusses socket programming in C. It begins with an introduction to sockets, describing them as an interface between applications and networks that allows processes to communicate. It then covers the client-server model and types of sockets like stream and datagram. The main socket APIs are explained, including functions for creating, binding, listening, connecting, sending, receiving and closing sockets. Examples of socket programming with UDP and TCP are provided, showing the typical functions used in each case. The document concludes by noting that files can also be sent over sockets and models for handling multiple clients like process, thread and worker pools.
This document provides an overview of Java sockets including how they allow for client-server communication over networks, the lifecycle of a socket server, and code examples for a socket server and clients. It discusses how sockets provide connection-oriented and connectionless services in Java using classes like ServerSocket and Socket. Diagrams depict the use cases and classes for a socket server that handles weather requests from multiple clients. Code for a WeatherSocketServer class and examples of client requests are also included.
This is all about socket programming in java using TCP and UDP socket and an example of simple Echo Server.
Also includes concepts of the socket, Socket class and methods and use of those.
This document provides an overview of socket programming in Java. It discusses how client-server applications use sockets to communicate over a network. Sockets are identified by an IP address and port number. The document explains TCP and UDP socket programming in Java. For TCP, it describes how the server creates a welcoming socket to accept client connections. For both TCP and UDP, it outlines the basic interactions between client and server sockets. The document concludes by noting that socket programming is easy in Java and real-time applications typically use threads to handle each socket.
The document discusses network programming and Java sockets. It introduces elements of client-server computing including networking basics like TCP, UDP and ports. It then covers Java sockets, explaining how to implement both a server and client using Java sockets. Code examples are provided of a simple server and client. The conclusion emphasizes that Java makes socket programming easier than other languages like C.
Overview of socket API.
A network socket is an interface for an application to connect to a host‘s network stack (part of the OS). Sockets provide an abstraction of the network. Applications use the transport services available on the socket interface for communication with a peer application.
Transport protocol such as TCP, UDP or SCTP offer different levels of service. TCP for example provides reliable stream-oriented transport service while UDP more reflects the best-effort kind of service provided by the underlying IP protocol layer.
Sockets are bound to a transport address. For TCP/IP sockets this means an IP address and port number.
Sockets provide only very basic data transport services without any higher level functions like parameter marshaling or serialization. These functions have to be performed by the applications. These functions are often hidden in middleware platforms like distributed object technologies (CORBA, RMI, DCOM) or web service frameworks (WCF, JAX-WS).
Multicast sockets make the IP multicast capability accessible to applications. Multicast sockets are often used for streaming services where multiple applications are recipients of the same data packets.
Sockets allow for two-way communication between hosts in a network. There are two types of sockets: server sockets and client sockets. Server sockets wait for connection requests from clients, while client sockets are used to send and receive data from servers. Sockets provide input and output streams for transmission of data between endpoints.
Introduction
This Tutorial is On Socket Programming In C Language for Linux. Instructions Give Below will only work On Linux System not in windows.
Socket API In windows is called Winsock and there we can discuss about that in another tutorial.
What is Socket?
Sockets are a method for communication between a client program and a server program in a network.
A socket is defined as "the endpoint in a connection." Sockets are created and used with a set of programming requests or "function calls" sometimes called the sockets application programming interface (API).
The most common sockets API is the Berkeley UNIX C interface for sockets.
Sockets can also be used for communication between processes within the same computer.
Socket programming in Java allows applications to communicate over the internet. Sockets are endpoints for communication that are identified by an IP address and port number. A socket connection is established between a client and server socket. The server creates a welcoming socket to accept client connection requests, then a separate connection socket to communicate with that client. Data can be sent bidirectionally over the connected sockets as input/output streams. UDP uses datagram sockets without a connection, requiring the explicit destination address on each message.
The document discusses sockets and the client-server model for interprocess communication. It describes what sockets are, the different types of sockets (STREAM, DATAGRAM, RAW), and how they are used for reliable and unreliable communication between processes. It provides code examples for creating, connecting, sending, receiving and closing sockets in both client and server applications using TCP and UDP. Key system calls for sockets like socket(), bind(), listen(), accept(), connect(), send(), recv(), close() are also explained.
This document discusses client-server programming and socket programming. It begins by explaining the client-server model, where the client initiates contact with the server to request a service. It then covers application layer programming using sockets as the interface. Sockets provide an abstraction for network communication, representing an endpoint. The document outlines functions for creating, binding, listening for connections, accepting connections, and sending/receiving data with sockets. It also discusses related topics like network byte order, utility functions, and address data structures.
The presentation given at MSBTE sponsored content updating program on 'Advanced Java Programming' for Diploma Engineering teachers of Maharashtra. Venue: Guru Gobind Singh Polytechnic, Nashik
Date: 22/12/2010
Session: Java Network Programming
The InetAddress class in Java represents IP addresses and allows conversion between host names and IP addresses. It has subclasses for IPv4 (Inet4Address) and IPv6 (Inet6Address) addresses. InetAddress objects store both the raw IP address and associated host name. Methods are provided to look up addresses by name, check address properties like scope, and test address reachability. Caching is used to improve performance of name lookups.
This document discusses Java networking and client/server communication. A client machine makes requests to a server machine over a network using protocols like TCP and UDP. TCP provides reliable data transmission while UDP sends independent data packets. Port numbers map incoming data to running processes. Sockets provide an interface for programming networks, with ServerSocket and Socket classes in Java. A server program listens on a port for client connections and exchanges data through input/output streams. Servlets extend web server functionality by executing Java programs in response to client requests.
This document discusses socket programming in Java. It begins by defining what a socket is - the combination of an IP address and port number used to uniquely identify an endpoint in a network connection. It then covers the basics of client-server socket programming using both TCP and UDP, including creating and using sockets, streams, and datagrams. Example code is provided for both TCP and UDP client and server implementations in Java using sockets to send and receive data. The document concludes with references for more information on socket programming.
The document describes 8 programs related to networking in Java. Program 1 obtains the IP address of a website. Program 2 connects to the home page of yahoo.com and displays the HTML. Program 3 parses a URL into its components. Program 4 translates between IP addresses and host names. Program 5 prints all IP addresses of a domain. Program 6 obtains the local machine's IP address. Program 7 finds the host name given an IP address. Program 8 downloads a file from the web and saves or displays it. The last part describes a client-server program with a server that echoes back a string sent by the client.
Socket programming allows processes to communicate over a network. Key aspects include:
- Demultiplexing packets into processes using port numbers
- Converting between host and network byte order
- Sockets representing an endpoint for communication and identified by address/port pairs
- Functions for binding addresses, listening for connections, accepting connections, and sending/receiving data over established sockets.
Socket programming allows applications on networked computers to communicate reliably using TCP or unreliably using UDP. A socket represents an open connection between two endpoints and has methods to get input/output streams, the remote host/port, and local port. A client socket connects to a server, while a server socket listens on a port and accepts connections from clients.
The document discusses network programming in Java using the java.net package. It covers using TCP and UDP for network communication. TCP provides reliable, ordered streams between hosts using sockets, while UDP provides simpler datagram transmission that is unreliable but can be broadcast. The InetAddress class represents IP addresses, and sockets are used for both TCP clients/servers and UDP communication.
The overview on Socket Programming that will lead you how to program with a short example of programming which is in slide. With the help of this you can understand about network programming.
The document discusses socket programming and provides code examples for implementing client-server applications in C. It introduces sockets and the client-server model, and describes how to create TCP and UDP sockets. It then shows the steps to program a TCP client and server in C, including creating/connecting sockets, sending/receiving data, and closing connections. Key system calls like socket(), bind(), listen(), accept(), send(), and recv() are explained.
This document discusses socket programming in C. It begins with an introduction to sockets, describing them as an interface between applications and networks that allows processes to communicate. It then covers the client-server model and types of sockets like stream and datagram. The main socket APIs are explained, including functions for creating, binding, listening, connecting, sending, receiving and closing sockets. Examples of socket programming with UDP and TCP are provided, showing the typical functions used in each case. The document concludes by noting that files can also be sent over sockets and models for handling multiple clients like process, thread and worker pools.
The document discusses socket programming and provides code examples for establishing sockets on both the client and server side. It explains that a socket is a communication mechanism that allows clients and servers to communicate locally or across networks. It then provides the key steps for setting up both connection-oriented and connectionless sockets, including creating and binding sockets, listening for connections, accepting connections, reading/writing data. It also discusses enhancements like allowing a server to handle multiple clients by forking new processes.
The document discusses network programming and the client-server model. It covers:
- The client-server exchange involves a client sending a request, the server handling the request and sending a response, and the client handling the response.
- Sockets provide a programming interface that allows network I/O to appear as file I/O. Clients and servers communicate by reading from and writing to socket file descriptors.
- Servers run as long-running daemon processes, listening on well-known ports for connection requests from clients. When a request is received, the server accepts the connection to form a connected socket for bidirectional data exchange.
The document discusses network programming and the client-server model. It begins with an overview of the client-server transaction process and the programmer's view of the Internet. It then covers topics like IP addresses, the Domain Name System (DNS), Internet connections using sockets, examples of clients and servers, and the sockets interface used for writing clients and servers.
The document discusses network programming and the client-server model. It provides details on:
1) The client-server transaction process where the client sends a request, the server handles it and sends a response which the client then handles.
2) The programmer's view of the internet including IP addresses, domain name system (DNS) mapping addresses to names, and internet connections between client and server processes.
3) Examples of client programs like web browsers and examples of server programs like web servers that provide services by manipulating resources for clients that connect to them.
This document describes a C program to implement a date-time server using TCP. The server program gets the system time, binds to a port, and sends the time string to any connected client. The client program connects to the server, receives the time string from the server, and prints it out. The programs successfully demonstrate a simple client-server model where the server provides the current date and time to multiple clients on request.
Sockets provide an abstraction for network communication between processes. They define endpoints for connections that are identified by IP addresses and port numbers. There are two main types - stream sockets which provide reliable connected service using TCP, and datagram sockets which provide unreliable datagram service using UDP. To use sockets, an application creates a socket, binds it to an address, listens/accepts for incoming connections, and sends/receives data. Key functions include socket(), bind(), listen(), accept(), connect(), send(), and recv(). Select() allows monitoring multiple sockets to avoid blocking. Proper use requires consideration of addressing, port numbers, error handling, and blocking behavior.
This document reports on Remote Procedure Call (RPC) and distributed systems. It provides background on RPC, describing it as a technique that allows a program to execute a subroutine in another address space, such as on another computer, without explicitly coding message passing details. It then gives timelines and information flows for how RPC works. The document also discusses socket programming as an implementation of RPC, showing code examples of a socket server and client that demonstrate how sockets allow message passing between processes similarly to RPC.
This document discusses socket programming in Java. It defines a socket as a combination of an IP address and port number that uniquely identifies each TCP connection. There are two types of sockets: stream sockets for reliable two-way communication and datagram sockets for unreliable connectionless communication. The document outlines the key socket calls like socket(), bind(), listen(), accept(), read(), write() for both TCP and UDP socket programming in client-server applications. It provides examples of how data is sent and received between the client and server using sockets.
Networking: Protocol, Sockets, Knowing IP Address, URL, Reading the Source Code of a Web Page, Downloading a Web Page from Internet, Downloading an Image from Internet, A TCP/IP Server, A TCP/IP Client, A UDP Server, A UDP Client, File Server, File Client, Two-Way Communication between Server and Client, Sending a Simple Mail,
This document provides an introduction to socket programming in C on Linux. Some key points:
- Sockets allow communication between processes over a network and act as an interface between applications and the network. They are represented by file descriptors.
- There are two main types of sockets - stream sockets (TCP) which provide reliable, ordered data transmission and datagram sockets (UDP) which are unreliable but do not require a connection.
- The socket API includes functions like socket(), bind(), listen(), accept(), connect(), recv(), send(), etc. to create and manage sockets and network communication.
- Data structures like sockaddr_in are used to store socket addresses containing IP and port. Byte ordering functions like
The document provides an overview of object oriented programming and network programming concepts. It discusses topics like IP addresses, ports, sockets, client-server programming, and the java.net and java.util packages. The java.net package contains classes for network programming in Java like Socket, ServerSocket, URL, and InetAddress. The java.util package contains general-purpose utility classes like ArrayList, HashMap, Properties and Date.
This document discusses sockets and socket programming in Java. It defines a socket as a combination of an IP address and port number that uniquely identifies each TCP connection on a network. There are two main types of sockets: stream sockets for reliable two-way communication and datagram sockets for unreliable datagrams. Socket programming uses functional calls like socket(), bind(), listen(), accept(), connect(), read(), write(), and close() to set up connections between client and server sockets over TCP. An example client-server app is described where the client sends a line to the server, which converts it to uppercase and sends it back to the client.
The document provides an overview of sockets and network programming in 3 paragraphs:
1) It introduces sockets as endpoints for connections between processes, with the socket represented by a descriptor integer. It describes how applications interface with sockets and the transport layer protocols like TCP and UDP.
2) It explains how connections between endpoints are defined by the host address (IP address) and port number. It provides details on port numbers and common port usages.
3) It outlines the main network transport protocols TCP and UDP, describing TCP as reliable and connection-based while UDP is unreliable and connectionless. It shows the typical client-server model using TCP.
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptsenthilnathans25
Sockets provide an abstraction for interprocess communication by defining endpoints for sending and receiving data between applications over a network. There are two main types of sockets - stream sockets which provide reliable connected service using TCP, and datagram sockets which provide unreliable datagram service using UDP. Sockets use addresses, ports, and the socket abstraction to allow applications to communicate over a network in a standardized way. The select function allows monitoring multiple sockets for read or write readiness to avoid blocking behavior.
This document provides an introduction to sockets and their use for interprocess communication. It defines sockets as an abstraction layer between applications and network protocols that allows processes to establish connections for sending and receiving data. The document outlines the key functions of sockets like defining endpoints, initiating and accepting connections, sending and receiving data, and terminating connections. It also describes the different types of sockets for reliable stream-based and unreliable datagram-based communication. Addressing, ports, and the client-server model are explained at a high level. Code snippets demonstrate how to create, bind, listen for, and accept connections on both the client and server side.
This document describes how to create UDP client-server programs in C using socket functions like sendto(), recvfrom(), and connect(). It discusses key differences between TCP and UDP, such as UDP being connectionless and unreliable. Example code is provided for a UDP echo server and client that exchange datagrams. Issues like lost datagrams, lack of flow control, and determining the outgoing interface are also covered. The document concludes by showing how to create a server that handles both TCP and UDP connections using select().
2. Overview
• Introduction to Sockets
• A generic Client-Server application
• Programming Client-Server in C
• Programming Client-Server in Java
• References
4. Introduction to Sockets
• Why Sockets?
– Used for Interprocess communication.
• The Client-Server model
– Most interprocess communication uses client-server model
– Client & Server are two processes that wants to communicate with each
other
– The Client process connects to the Server process, to make a request for
information/services own by the Server.
– Once the connection is established between Client process and Server
process, they can start sending / receiving information.
• What are Sockets?
– End-point of interprocess communication.
– An interface through which processes can
send / receive information
Socket
5. Introduction to Sockets
• What exactly creates a Socket?
– <IP address, Port #> tuple
• What makes a connection?
– {Source<IP address, Port #> , Destination <IP address, Port #>} i.e. source
socket – destination socket pair uniquely identifies a connection.
• Example
Server
Client
Client
192.168.0.1
192.168.0.2
192.168.0.2
80
1343
5488
Client
192.168.0.3
1343
6. Introduction to Sockets
• Socket Types
– STREAM – uses TCP which is reliable, stream oriented protocol
– DATAGRAM – uses UDP which is unreliable, message oriented protocol
– RAW – provides RAW data transfer directly over IP protocol (no transport
layer)
• Sockets can use
– “unicast” ( for a particular IP address destination)
– “multicast” ( a set of destinations – 224.x.x.x)
– “broadcast” (direct and limited)
– “Loopback” address i.e. 127.x.x.x
8. A generic TCP application
• algorithm for TCP client
– Find the IP address and port number of server
– Create a TCP socket
– Connect the socket to server (Server must be up and listening for new
requests)
– Send/ receive data with server using the socket
– Close the connection
• algorithm for TCP server
– Find the IP address and port number of server
– Create a TCP server socket
– Bind the server socket to server IP and Port number (this is the port to which
clients will connect)
– Accept a new connection from client
• returns a client socket that represents the client which is connected
– Send/ receive data with client using the client socket
– Close the connection with client
9. A generic UDP application
• algorithm for UDP client
– Find the IP address and port number of server
– Create a UDP socket
– Send/ receive data with server using the socket
– Close the connection
• algorithm for UDP server
– Find the IP address and port number of server
– Create a UDP server socket
– Bind the server socket to server IP and Port number (this is the port to which
clients will send)
– Send/ receive data with client using the client socket
– Close the connection with client
11. Programming Client-Server in C
• The steps involved in establishing a socket on the client side are as
follows:
– Create a socket with the socket() system call
– Connect the socket to the address of the server using the connect() system
call
– Send and receive data using send() and recv() system calls.
• The steps involved in establishing a socket on the server side are as
follows:
– Create a socket with the socket() system call
– Bind the socket to an address using the bind() system call. For a server
socket on the Internet, an address consists of a port number on the host
machine.
– Listen for connections with the listen() system call
– Accept a connection with the accept() system call. This call typically blocks
until a client connects with the server.
– Send and receive data
12. Programming TCP Client in C
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(char *msg){ perror(msg); exit(0);}
int main(int argc, char *argv[]){
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname portn", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0) error("ERROR opening socket");
/* a structure to contain an internet address
defined in the include file <netinet/in.h> */
struct sockaddr_in {
short sin_family; /* should be AF_INET */
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8]; /* not used, must be zero
*/
};
struct in_addr {
unsigned long s_addr;
};
Client.c
13. Programming TCP Client in C
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(char *msg){ perror(msg); exit(0);}
int main(int argc, char *argv[]){
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname portn", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0) error("ERROR opening socket");
Client.c Socket System Call – create an end point for
communication
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
Returns a descriptor
domain: selects protocol family
e.g. PF_IPX, PF_X25, PF_APPLETALK
type: specifies communication semantics
e.g. SOCK_DGRAM, SOCK_RAW
protocol: specifies a particular protocol to be used
e.g. IPPROTO_UDP, IPPROTO_ICMP
14. Programming TCP Client in C
server = gethostbyname(argv[1]);
if (server == NULL) { fprintf(stderr,"ERROR, no such hostn"); exit(0); }
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr
, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256); fgets(buffer,255,stdin);
n = send(sockfd,buffer,strlen(buffer),0);
if (n < 0) error("ERROR writing to socket");
bzero(buffer,256);
n = recv(sockfd,buffer,255,0);
if (n < 0)
error("ERROR reading from socket");
printf("%sn",buffer);
close(sockfd);
return 0;
}
Client.c
Connect System Call – initiates a connection on
a socket
#include <sys/types.h>
#include <sys/socket.h>
int connect( int sockfd,
const struct sockaddr *serv_addr,
socklen_t addrlen);
Returns 0 on success
sockfd: descriptor that must refer to a socket
serv_addr: address to which we want to connect
addrlen: length of serv_addr
15. Programming TCP Client in C
server = gethostbyname(argv[1]);
if (server == NULL) { fprintf(stderr,"ERROR, no such hostn"); exit(0); }
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr
, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256); fgets(buffer,255,stdin);
n = send(sockfd,buffer,strlen(buffer),0);
if (n < 0) error("ERROR writing to socket");
bzero(buffer,256);
n = recv(sockfd,buffer,255,0);
if (n < 0)
error("ERROR reading from socket");
printf("%sn",buffer);
close(sockfd);
return 0;
}
Client.c
Send System Call – send a message to a
socket
#include <sys/types.h>
#include <sys/socket.h>
int send( int s, const void *msg, size_t len,
int flags);
Returns number of characters sent on success
s: descriptor that must refer to a socket in
connected state
msg: data that we want to send
len: length of data
flags: use default 0. MSG_OOB, MSG_DONTWAIT
16. Programming TCP Client in C
server = gethostbyname(argv[1]);
if (server == NULL) { fprintf(stderr,"ERROR, no such hostn"); exit(0); }
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr
, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256); fgets(buffer,255,stdin);
n = send(sockfd,buffer,strlen(buffer),0);
if (n < 0) error("ERROR writing to socket");
bzero(buffer,256);
n = recv(sockfd,buffer,255,0);
if (n < 0)
error("ERROR reading from socket");
printf("%sn",buffer);
close(sockfd);
return 0;
}
Client.c
Recv System Call – receive a message from a
socket
#include <sys/types.h>
#include <sys/socket.h>
int recv( int s, const void *buff, size_t len,
int flags);
Returns number of bytes received on success
s: descriptor that must refer to a socket in
connected state
buff: data that we want to receive
len: length of data
flags: use default 0. MSG_OOB, MSG_DONTWAIT
17. Programming TCP Client in C
server = gethostbyname(argv[1]);
if (server == NULL) { fprintf(stderr,"ERROR, no such hostn"); exit(0); }
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr
, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256); fgets(buffer,255,stdin);
n = send(sockfd,buffer,strlen(buffer),0);
if (n < 0) error("ERROR writing to socket");
bzero(buffer,256);
n = recv(sockfd,buffer,255,0);
if (n < 0)
error("ERROR reading from socket");
printf("%sn",buffer);
close(sockfd);
return 0;
}
Client.c
Close System Call – close a socket descriptor
#include <unistd.h>
int close( int s);
Returns 0 on success
s: descriptor to be closed
18. Programming TCP Server in C
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg){ perror(msg); exit(0);}
int main(int argc, char *argv[]){
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) { fprintf(stderr,"ERROR, no port providedn"); exit(1); }
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
Server.c
19. Programming TCP Server in C
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error("ERROR on accept");
bzero(buffer,256);
n = recv(newsockfd,buffer,255,0);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %sn",buffer);
n = send(newsockfd,"I got your message",18,0);
if (n < 0) error("ERROR writing to socket");
close(newsockfd);
close(sockfd);
return 0;
}
Server.c
Bind System Call – bind a name to a socket
#include <sys/types.h>
#include <sys/socket.h>
int bind( int sockfd,
const struct sockaddr *serv_addr,
socklen_t addrlen);
Returns 0 on success
sockfd: descriptor that must refer to a socket
serv_addr: address to which we want to connect
addrlen: length of serv_addr
20. Programming TCP Server in C
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error("ERROR on accept");
bzero(buffer,256);
n = recv(newsockfd,buffer,255,0);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %sn",buffer);
n = send(newsockfd,"I got your message",18,0);
if (n < 0) error("ERROR writing to socket");
close(newsockfd);
close(sockfd);
return 0;
}
Server.c
Listen System Call – listen for connections on a
socket
#include <sys/types.h>
#include <sys/socket.h>
int listen( int s, int backlog);
Returns 0 on success
s: descriptor that must refer to a socket
backlog: maximum length the queue for completely
established sockets waiting to be accepted
addrlen: length of serv_addr
21. Programming TCP Server in C
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) error("ERROR on accept");
bzero(buffer,256);
n = recv(newsockfd,buffer,255,0);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %sn",buffer);
n = send(newsockfd,"I got your message",18,0);
if (n < 0) error("ERROR writing to socket");
close(newsockfd);
close(sockfd);
return 0;
}
Server.c
Accept System Call – accepts a connection on
a socket
#include <sys/types.h>
#include <sys/socket.h>
int accept( int sockfd,
const struct sockaddr *addr,
socklen_t addrlen);
Returns a non-negative descriptor on success
sockfd: descriptor that must refer to a socket
addr: filled with address of connecting entity
addrlen: length of addr
22. Programming UDP Client in C
• The client code for a datagram socket client is the same as that for a
stream socket with the following differences.
– the socket system call has SOCK_DGRAM instead of SOCK_STREAM as
its second argument & IPPROTO_UDP instead of IPPROTO_TCP as its
third argument.
– there is no connect() system call
– instead of send() and recv(), the client uses sendto() and recvfrom()
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
len = sizeof(struct sockaddr_in);
while (1) {
/* write */
n = sendto(sock,“Got your messagen",17, 0,(struct sockaddr *) &server, len);
f (n < 0) error("sendto");
/* read */
n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from, len);
if (n < 0) error("recvfrom");
}
23. Programming UDP Server in C
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
len = sizeof(struct sockaddr_in);
while (1) {
/* read */
n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from, len);
if (n < 0) error("recvfrom");
/* write */
n = sendto(sock,"Got your messagen",17, 0,(struct sockaddr *)&from, len);
f (n < 0) error("sendto");
}
• Server code with a datagram socket is similar to the stream socket
code with following differences.
– Servers using datagram sockets do not use the listen() or the accept()
system calls.
– After a socket has been bound to an address, the program calls
recvfrom() to read a message or sendto() to send a message.
24. Programming Client-Server in C
#include <winsock.h>
…..
void main(int argc,char *argv[]){
WSADATA wsda; // if this doesn’t work
// WSAData wsda; // then try this
WSAStartup(0x0101,&wsda);
…..
WSACleanup();
closesocket(sockfd);
}
• In case of Windows Everything in the code is same as described
previously except the following differences
– You have to tell your compiler to link in the Winsock library, usually
called wsock32.lib or winsock32.lib
– On Visual C++, this can be done through the Project menu, under
Settings.... Click the Link tab, and look for the box titled "Object/library
modules". Add "wsock32.lib" to that list.
– On Visual Studio .NET, add “wsock32.lib” under Project menu, Properties
-> Linker -> Input -> Additional Dependencies
26. Programming TCP Client-Server in Java
• All the classes related to sockets are in the java.net package, so make
sure to import that package when you program sockets.
• All the input/output stream classes are in the java.io package, include
this also
• How to open a socket?
– If you are programming a client, then you would create an object of
Socket class
– Machine name is the machine you are trying to open a connection to,
– PortNumber is the port (a number) on which the server you are trying to
connect to is running. select one that is greater than 1,023! Why??
Socket MyClient;
try {
MyClient = new Socket("Machine name",
PortNumber);
}
catch (IOException e) {
System.out.println(e);
}
27. Programming TCP Client-Server in Java
• If you are programming a server, then this is how you open a socket:
• When implementing a server you also need to create a socket object
from the ServerSocket in order to listen for and accept connections
from clients.
ServerSocket MyService;
try {
MyServerice = new ServerSocket(PortNumber);
}
catch (IOException e) {
System.out.println(e);
}
Socket clientSocket = null;
try {
clientSocket = MyService.accept();
}
catch (IOException e) {
System.out.println(e);
}
28. Programming TCP Client-Server in Java
• How to create an input stream?
– On the client side, you can use the DataInputStream class to create an
input stream to receive response from the server:
– The class DataInputStream allows you to read lines of text and Java
primitive data types in a portable way. It has methods such as read,
readChar, readInt, readDouble, and readLine,.
– On the server side, you can use DataInputStream to receive input from
the client:
DataInputStream input;
try {
input = new DataInputStream(MyClient.getInputStream());
}
catch (IOException e) {
System.out.println(e);
}
DataInputStream input;
try {
input = new
DataInputStream(clientSocket.getInputStream());
}
catch (IOException e) {
System.out.println(e);
}
29. Programming TCP Client-Server in Java
• How to create an output stream?
– On the client side, you can create an output stream to send information
to the server socket using the class PrintStream or DataOutputStream
of java.io:
– The class PrintStream has methods for displaying textual representation
of Java primitive data types. Its write and println methods are important.
Also, you may want to use the DataOutputStream:
PrintStream output;
try {
output = new PrintStream(MyClient.getOutputStream());
}
catch (IOException e) {
System.out.println(e);
}
DataOutputStream output;
try {
output = new
DataOutputStream(MyClient.getOutputStream());
}
catch (IOException e) {
System.out.println(e);
}
30. Programming TCP Client-Server in Java
• On the server side
– you can use the class PrintStream to send information to the client.
• Note: You can use the class DataOutputStream as mentioned previously.
PrintStream output;
try {
output = new
PrintStream(clientSocket.getOutputStream());
}
catch (IOException e) {
System.out.println(e);
}
31. Programming TCP Client-Server in Java
• How to close sockets?
– You should always close the output and input stream before you close
the socket.
– On the client side:
– On the server side:
try {
output.close();
input.close();
MyClient.close();
}
catch (IOException e) {
System.out.println(e);
}
try {
output.close();
input.close();
clientSocket.close();
MyService.close();
}
catch (IOException e) {
System.out.println(e);
}
32. Programming UDP Client-Server in Java
• How to open a datagram socket?
– If you are programming a client, then you would create an object of
DatagramSocket class
• If you are programming a server, then this is how you open a socket:
try {
DatagramSocket socket = new DatagramSocket();
}
catch (IOException e) {
System.out.println(e);
}
DatagramSocket socket = null;
try {
socket = new DatagramSocket(4445);
}
catch (IOException e) {
System.out.println(e);
}
33. Programming UDP Client-Server in Java
• How to send/receive on Datagram sockets?
– On the client side, you can use the DatagramPacket class
– To send data
– To receive data
byte[] buf = new byte[256];
InetAddress address = InetAddress.getByName(args[0]);
DatagramPacket packet = new DatagramPacket(buf,
buf.length, address, 4445);
socket.send(packet);
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
String received = new String(packet.getData());
System.out.println(“Received from server: " + received);
34. Programming UDP Client-Server in Java
• How to send/receive on Datagram sockets?
– On the Server side, you can use the DatagramPacket class
– To receive data
• To send data
• How to close a Datagram socket?
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf,
buf.length);
socket.receive(packet);
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);
socket.close();
35. References
• Man pages in Linux
Accesssible through following command
– man 2 <system_call_name>
– E.g. man 2 socket
• “Unix network programming” by Richard Stevens
• Beej’s guide to Network Programming
http://beej.us/guide/bgnet/
• The Java Tutorial – Custom Networking
http://java.sun.com/docs/books/tutorial/networking/
• Lecture notes of cs423 from Dr. Bob Cotter
http://www.sce.umkc.edu/~cotterr/cs423_fs05/cs423_fs05_lectures.html