Laws Tcp/ip Sockets In C# Pdf


Monday, July 1, 2019

Power up your mind: learn faster, work smarter / Bill Lucas. p. cm. ways in which you can power up your mind and impr. IP Addresses in C#. Using C# Sockets. C# Socket Exceptions. . C# Socket Helper Classes. TcpClient. TcpListener. UdpClient. TCP/IP Sockets in C#. TCP/IP Sockets in C# - 1st Edition - ISBN: , Price includes VAT/GST. DRM-free (EPub, PDF, Mobi).

Tcp/ip Sockets In C# Pdf

Language:English, Spanish, German
Published (Last):28.03.2016
ePub File Size:19.56 MB
PDF File Size:11.57 MB
Distribution:Free* [*Regsitration Required]
Uploaded by: ANALISA

TCP/IP Sockets in Java: Practical Guide for Programmers. Kenneth L. Originally published as The Pocket Guide to TCP/IP Sockets: C Version ( ). Chapter 7, Using the C# Socket Helper Classes, discusses the C# TcpClient, . Chapter 1: The C# Language Chapter 2: IP Programming Basics Chapter 3: C#. TCP/IP Sockets in Java: Practical Guide for Programmers. Kenneth L. .. You are now ready to learn to write your own socket applications in C#. One of the.

NET Framework using C. Secondly, we will create a small application consisting of a server and a client which will communicate using TCP and UDP protocols. Introduction In this article, we will learn the basics of socket programming in. Secondly, we will create a small application consisting of a server and a client, which will communicate using TCP and UDP protocols.

Pre-requisites Must be familiar with.

NET Framework. Should have good knowledge of C. Instances of InetAddress are immutable: To get the addresses of the local host, the program takes advantage of the Network Interface abstraction.

Recall that IP addresses are actually assigned to the connection between a host and a network and not to the host itself. This is extremely useful, for example when a program needs to inform another program of its address.

Enumeration; import java. Check for empty list: Get and print address es of each interface in the list: At this time the only subtypes of InetAddress are those listed, but conceivably there might be others someday. The 12 Chapter 2: Catch exception: Get names and addresses for each command-line argument: Address v4: Unable to find address for blah. Link-local IPv6 addresses begin with fe8. You may also have noticed a delay when resolving blah.

Your resolver looks in several places before giving up on resolving a name. When the name service is not available for some reason—say, the program is running on a machine that is not connected to any network— attempting to identify a host by name may fail. It is, therefore, good to know that you can always refer to a host using the IP address in dottedquad notation. If you can ping a host using one of its names e. If your ping test fails or 2.

See also the isReachable method of InetAddress, discussed below. For numeric IPv6 addresses, the shorthand forms described in Chapter 1 may be used. A name may be associated with more than one numeric address; the getAllByName method returns an instance for each address associated with a name. The getAddress method returns the binary form of the address as a byte array of appropriate length. If the instance is of Inet4Address, the array is four bytes in length; if of Inet6Address, it is 16 bytes.

As we have seen, an InetAddress instance may be converted to a String representation in several ways. The numeric representation of the address only is returned by getHostAddress. For an IPv6 address, the string representation always includes the full eight groups i.

Both methods return the numerical form of the address if resolution cannot be completed. Also, both check permission with the security manager before sending any messages. They all work for both IPv4 and IPv6 addresses. The fourth method checks whether it is a multicast address see Section 4. The scope determines, roughly, how far packets addressed to that destination can travel from their origin.

Note that, unlike the other methods, which involve simple syntactic checks, these methods cause the networking system to take action, namely 2. The TTL limits the distance a packet can travel through the network.

The NetworkInterface class provides a large number of methods, many of which are beyond the scope of this book. We describe here the most useful ones for our purposes. Similarly, the list of addresses may contain linklocal addresses that also are not globally reachable.

The getName methods return the name of the interface not the host. This generally consists of an alphabetic string followed by a numeric part, for example eth0. The loopback interface is named lo0 on many systems.

Socket and ServerSocket. An instance of Socket represents one end of a TCP connection. An instance of ServerSocket listens for TCP connection requests and creates a new Socket instance to handle each incoming connection. Thus, servers handle both ServerSocket and Socket instances, while clients use only Socket. We begin by examining an example of a simple client. The typical TCP client goes through three steps: Construct an instance of Socket: Close the connection using the close method of Socket.

An echo server simply repeats whatever it receives back to the client. The string to be echoed is provided as a command-line argument to our client. Some systems include an echo server for debugging and testing purposes.

You may be able to use a program such as telnet to test if the standard echo server is running on your system e. Socket; java. SocketException; java. IOException; java.

InputStream; java. Application setup and parameter parsing: The getBytes method of String returns a byte array representation of the string. See Section 3. If we specify a third parameter, Integer. TCP socket creation: Note that the underlying TCP 18 Chapter 2: Basic Sockets deals only with IP addresses; if a name is given, the implementation resolves it to the corresponding address.

If the connection attempt fails for any reason, the constructor throws an IOException. Get socket input and output streams: We send data over the socket by writing bytes to the OutputStream just as we would any other stream, and we receive by reading from the InputStream.

Send the string to echo server: Receive the reply from the echo server: This particular form of read takes three parameters: For the client, this indicates that the server prematurely closed the socket. Why not just a single read? TCP does not preserve read and write message boundaries. That is, even though we sent the echo string with a single write , the echo server may receive it in multiple chunks.

Even if the echo string is handled in one chunk by the echo server, the reply may still be broken into pieces by TCP. One of the most common errors for beginners is the assumption that data sent by a single write will always be received in a single read. Print echoed string: Close socket: We can communicate with an echo server named server.

Echo this! Specifying the local address may be useful on a host with multiple interfaces. String arguments that specify destinations can be in the same formats that are accepted by the InetAddress creation methods. The last constructor creates an unconnected socket, which must be explicitly connected via the connect method, see below before it can be used for communication. Stream methods. Any subsequent attempt to read from the socket will cause an exception to be thrown.

See Section 4. By default, Socket is implemented on top of a TCP connection; however, in Java, you can actually change the underlying implementation of Socket. The Socket class actually has a large number of other associated attributes referred to as socket options. Because they are not necessary for writing basic applications, we postpone introduction of them until Section 4. The constructor that takes a string hostname attempts to resolve the name to an IP address; the 2.

The isUnresolved method returns true if the instance was created this way, or if the resolution attempt in the constructor failed. The get If the InetSocketAddress is unresolved, only the String with which it was created precedes the colon.

The typical TCP server goes through two steps: Construct a ServerSocket instance, specifying the local port. Call the accept method of ServerSocket to get the next incoming client connection. Upon establishment of a new client connection, an instance of Socket for the new connection is created and returned by accept. The server is very simple. It runs forever, repeatedly accepting a connection, receiving and echoing bytes until the connection is closed by the client, and then closing the client socket.

Basic Sockets if args. We are done with this client! Server socket creation: Loop forever, iteratively handling incoming connections: If a connection arrives between the time the server socket is constructed and the call to accept , the new connection is queued, and in that case accept returns immediately.

See Section 6. The accept method of ServerSocket returns an instance of Socket 2. The name part is empty because the instance was created from the address information only. The read method of InputStream fetches up to the maximum number of bytes the array can hold in this case, BUFSIZE bytes into the byte array receiveBuf and returns the number of bytes read.

In fact, it can return after having read only a single byte. In this case, 0 indicates to take bytes starting from the front of data. Valid port numbers are in the range 0—65, Optionally, the size of the connection queue and the local address can also be set. Note that the maximum queue size may not be a hard limit, and cannot be used to control client population. This may be useful for hosts with multiple interfaces where the server wants to accept connections on only one of its interfaces.

The fourth constructor creates a ServerSocket that is not associated with any local port; it must be bound to a port see bind below before it can be used.

Operation void bind int port void bind int port, int queuelimit Socket accept void close The bind methods associate this socket with a local port. A ServerSocket can only be associated with one port. If no established connection is waiting, accept blocks until one is established or a timeout occurs. The close method closes the socket.

After invoking this method, incoming client connection requests for this socket are rejected. It does, however, have other attributes called options, which can be controlled via various methods, as described in Section 4. The NIO facilities, added in Java 1. A stream is simply an ordered sequence of bytes. Java input streams support reading bytes, and output streams support writing bytes. When we write to the output stream of a Socket, the bytes can eventually be read from the input stream of the Socket at the other end of the connection.

OutputStream is the abstract superclass of all output streams in Java. Operation abstract void write int data void write byte[ ] data void write byte[ ] data, int offset, int length void flush void close The write methods transfer to the output stream a single byte, an entire array of bytes, and the bytes in an array beginning at offset and continuing for length bytes, respectively.

The single-byte method writes the low-order eight bits of the integer argument. These operations, if called on a stream associated with a TCP socket, may block if a lot of data has been sent, but the other end of the connection has not called read on the associated input stream recently.

This can have undesirable consequences if some care is not used see Section 6. The close method terminates the stream, after which further calls to write will throw an exception.

InputStream is the abstract superclass of all input streams. Using an InputStream, we can read bytes from and close the input stream.

Operation abstract int read int read byte[ ] data int read byte[ ] data, int offset, int length int available void close 26 Chapter 2: The second form transfers up to data. The third form does the same, but places data in the array beginning at offset, and transfers only up to length bytes. If no data is available, but the end-of-stream has not been detected, all the read methods block until at least one byte can be read. The available method returns the number of bytes available for reading at the time it was called.

In fact, UDP performs only two functions: For example, UDP sockets do not have to be connected before being used. Similarly, each message—called a datagram—carries its own address information and is independent of all others.

UDP sockets preserve them. This makes receiving an application message simpler, in some ways, than it is with TCP sockets. This is discussed further in Section 2. A program using UDP sockets must therefore be prepared to deal with loss and reordering. Both clients and servers use DatagramSockets to send and receive DatagramPackets.

To send, a Java program constructs a DatagramPacket instance containing the data to be sent and passes it as an argument to the send method of a DatagramSocket. In addition to the data, each instance of DatagramPacket also contains address and port information, the semantics of which depend on whether the datagram is being sent or received.

When a DatagramPacket is sent, the address and port identify the destination; for a received DatagramPacket, they identify the source of the received message.

See the following reference and Section 2. Creation DatagramPacket byte[ ] DatagramPacket byte[ ] DatagramPacket byte[ ] DatagramPacket byte[ ] DatagramPacket byte[ ] DatagramPacket byte[ ] data, data, data, data, data, data, int int int int int int length offset, int length length, InetAddress remoteAddr, int remotePort offset, int length, InetAddress remoteAddr, int remotePort length, SocketAddress sockAddr offset, int length, SocketAddress sockAddr These constructors create a datagram whose data portion is contained in the given byte array.

The last four forms are typically used to construct DatagramPackets for sending. The internal datagram length can be set explicitly either by the constructor or by the setLength method. The receive method of DatagramSocket uses the internal length in two ways: There is no setOffset method; however, it can be set with setData.

The getData method returns the byte array associated with the datagram. The returned object is a reference to the byte array that was most recently associated with this DatagramPacket, either by the constructor or by setData. The setData methods make the given byte array the data portion of the datagram. The typical UDP client goes through three steps: Construct an instance of DatagramSocket, optionally specifying the local address and port.

Communicate by sending and receiving instances of DatagramPacket using the send and receive methods of DatagramSocket. A UDP echo server simply sends each datagram that it receives back to the client.

Many systems include a UDP echo server for debugging and testing purposes.

TCP-IP Sockets in Java. Practical Guide for Programmers

One consequence of using UDP is that datagrams can be lost. In the case of our echo protocol, either the echo request from the client or the echo reply from the server may be lost in the network. Recall that our TCP echo client sends an echo string and then blocks on read waiting for a reply. If we try the same strategy with our UDP echo client and the echo request datagram is lost, our client will block forever on receive.

To avoid this problem, our client 30 Chapter 2: Basic Sockets uses the setSoTimeout method of DatagramSocket to specify a maximum amount of time to block on receive , so it can try again by resending the echo request datagram. Our echo client performs the following steps: Send the echo string to the server.

Terminate the client. DatagramSocket; java. DatagramPacket; java. InetAddress; java. Application setup and parameter processing: UDP socket creation: We do not specify a local address or port so some local address and available port will be selected. We could explicitly set them with the setLocalAddress and setLocalPort methods or in the constructor, if desired. Set the socket timeout: Here we set the timeout to three seconds. Note that timeouts are not precise: Create datagram to send: For the destination address, we may identify the echo server either by name or IP address.

If we specify a name, it is converted to the actual IP address in the constructor. Basic Sockets 5. Create datagram to receive: Send the datagram: Timer expiration is indicated by an InterruptedIOException. If the timer expires, we increment the send attempt count tries and start over. After the maximum number of tries, the while loop exits without receiving a datagram.

An Introduction to Socket Programming in .NET using C#

Print reception results: Close the socket: A socket connected to a multicast or broadcast address can only send datagrams because a datagram source address is always a unicast address see Section 4.

Note that connecting is strictly a local operation because unlike TCP there is no end-to-end packet exchange involved. The close method indicates that the socket is no longer in use; further attempts to send or receive throw an exception. The third method returns both address and port conveniently encapsulated in an instance of SocketAddress, or null if unconnected. The last three methods provide the same service for the local address and port.

The getLocalSocketAddress returns null if the socket is not bound. Sending and receiving void send DatagramPacket packet void receive DatagramPacket packet 34 Chapter 2: Basic Sockets The send method sends the DatagramPacket. Otherwise, the packet is sent to the destination indicated by the DatagramPacket.

This method does not block. The receive method blocks until a datagram is received, and then copies its data into the given DatagramPacket. If the socket is connected, the method blocks until a datagram is received from the remote socket to which it is connected.

Options int getSoTimeout void setSoTimeout int timeoutMillis These methods return and set, respectively, the maximum amount of time that a receive call will block for this socket. If the timer expires before data is available, an InterruptedIOException is thrown.

The timeout value is given in milliseconds. They are described more fully in Section 4. The typical UDP server goes through three steps: Construct an instance of DatagramSocket, specifying the local port and, optionally, the local address. The server is now ready to receive datagrams from any client. Receive an instance of DatagramPacket using the receive method of DatagramSocket.

Communicate by sending and receiving DatagramPackets using the send and receive methods of DatagramSocket. The server is very simple: IOException; import java. DatagramPacket; import java. Create and set up datagram socket: Create datagram: This datagram will be used both to receive the echo request and to send the echo reply. Basic Sockets 4. Iteratively handle incoming echo requests: If we do not reset the internal length before receiving again, the next message will be truncated if it is longer than the one just received.

Each call to receive on a DatagramSocket returns data from at most one call to send. This is covered in more detail in Chapter 6.

This means that by the time a call to send returns, the message has been passed to the underlying channel for transmission and is or soon will be on its way out the door. With a connected TCP socket, all received-but-not-yet-delivered bytes are treated as one continuous sequence of bytes see Chapter 6.

A call to receive will never return more than one message. The remaining bytes are quietly discarded, with no indication to the receiving program that information has been lost! This technique will guarantee that no data will be lost. The maximum amount of data that can be transmitted in a DatagramPacket is 65, bytes—the largest payload that can be carried in a UDP datagram.

For example, suppose buf is a byte array of size 20, which has been initialized so that each byte contains its index in the array: The message is received into dg: One possibility is to copy the received data into a separate byte array, like this: Basic Sockets As of Java 1.

We said that a socket must have a port for communication, yet we do not specify a port in TCPEchoClient. What happens if a TCP server never calls accept? What happens if a TCP client sends data on a socket that has not yet been accept ed at the server? Servers are supposed to run for a long time without stopping—therefore, they must be designed to provide good service no matter what their clients do.

What is happening? Note that the response could vary by OS. What happens? Verify experimentally the size of the largest message you can send and receive using a DatagramPacket.

There is no magic: This agreement regarding the form and meaning of information exchanged over a communication channel is called a protocol; a protocol used in implementing a particular application is an application protocol. In our echo example from the earlier chapters, the application protocol is trivial: Because in most real applications the behavior of clients and servers depends upon the information they exchange, application protocols are usually somewhat more complicated.

So from now on we consider messages to be sequences of bytes. Given this, it may be helpful to think of a transmitted message as a sequence or array of numbers, each between 0 and That corresponds to the range of binary values that can be encoded in 8 bits: Sending and Receiving Data When you build a program to exchange information via sockets with other programs, typically one of two situations applies: We have seen that bytes of information can be transmitted through a socket by writing them to an OutputStream associated with a Socket or encapsulating them in a DatagramPacket which is then sent via a DatagramSocket.

However, the only data types to which these operations can be applied are bytes and arrays of bytes. As a strongly typed language, Java requires that other types—int, String, and so on—be explicitly converted to byte arrays. Fortunately, the language has built-in facilities to help with such conversions. We saw one of these in Section 2.

Using that ability, we can encode the values of other larger primitive integer types. One is the size in bytes of each integer to be transmitted. For example, an int value in a Java program is represented as a bit quantity.

We can therefore transmit the value of any variable or constant of type int using four bytes. Values of type short, on the other hand, are represented using 16 bits and so only require two bytes to transmit, while longs are 64 bits or eight bytes.

We need a total of 15 bytes: Not quite. For types that require more than one byte, we have to answer the question of which order to send the bytes in. There are two obvious choices: Note that the ordering of bits within bytes is, fortunately, handled by the implementation in a standard way. Consider the long value L.

Its bit representation in hexadecimal is 0xFB1. If we transmit the bytes in big-endian order, the sequence of decimal byte values will look like this: One last detail on which the sender and receiver must agree: Chapter 5 contains further details about this class. Sending and Receiving Data it represents 4, , , Because Java does not support unsigned integer types, encoding and decoding unsigned numbers in Java requires a little care. Assume for now that we are dealing with signed integer types.

So how do we get the correct values into the byte array of the message? The program BruteForceCoding. If we encode at the sender, we must be able to decode at the receiver. SIZE; Short. SIZE; Integer. SIZE; Long. Untested preconditions e. Data items to encode: Numbers of bytes in Java integer primitives: BYTEMASK keeps the byte value from being sign-extended when it is converted to an int in the call to append , thus rendering it as an unsigned integer. Sending and Receiving Data 4. The resulting value is then cast to the type byte, which throws away all but the low-order eight bits, and placed in the array at the appropriate location.

This is iterated over size bytes of the given value, val. Demonstrate methods: If we place the return value into a long, it simply becomes the last byte of a long, producing a value of Which answer is correct depends on your application.

If you expect a signed value from decoding N bytes, you must place the long result in a primitive integer type that uses exactly N bytes. Can you name any others? Running the program produces output showing the following decimal byte values: It would be even worse if the encodeIntBigEndian method were not factored out as a separate method. For that reason, it is not the recommended approach, because Java provides some built-in mechanisms that are easier to use.

Note that it does have 3. The ByteArrayOutputStream class takes the sequence of bytes written to a stream and converts it to a byte array. The code for building our message looks like this: So much for the sending side.

How does the receiver recover the transmitted values? Finally, essentially everything in this subsection applies also to the BigInteger class, which supports arbitrarily large integers. However, this defeats the purpose of using a BigInteger, which can be arbitrarily large.

Text is convenient because humans are accustomed to dealing with all kinds of information represented as strings of characters in books, newspapers, and on computer displays. Thus, once we know how to encode text for transmission, we can send almost any other kind of data: Obviously we can represent numbers and boolean values as Strings—for example "", "6.

Alas, there is more to it than that. In fact every String instance corresponds to a sequence array of characters type char[ ]. A char value in Java is represented internally as an integer. The character "X" corresponds to 88, and the symbol "!

A mapping between a set of symbols and a set of integers is called a coded character set.

Kumpulan 1150+ Link Ebook Pemrograman Gratis (Sedot Semua!)

ASCII maps the letters of the English alphabet, digits, punctuation and some other special non-printable symbols to integers between 0 and It has been used for data transmission since the s, and is used extensively in application protocols such as HTTP the protocol used for the World Wide Web , even today. Java therefore uses an international standard coded character set called Unicode to represent values of type char and String.

So sender and receiver have to agree on a mapping from symbols to integers in order to communicate using text messages. Is that all they need to agree on? It depends. For a small set of characters with no integer value larger than , nothing more is needed because each character can be encoded as a single byte.

For a code that may use larger integer values that require more than a single byte to represent, there is more than one way to encode those values on the wire.

Thus, sender and receiver need to agree on how those integers will be represented as byte sequences—that is, an encoding scheme. The combination of a coded character set and a character encoding scheme is called a charset see RFC Java provides support for the use of arbitrary charsets, and every implementation is required to support at least the following: When you invoke the getBytes method of a String instance, it returns a byte array containing the String encoded according to the default charset for the platform.

To ensure that a string is encoded using a particular charset, you simply supply the name of the charset as a String argument to the getBytes method. The resulting byte array contains the representation of the string in the given encoding. If you call "Test! From "Test! The easiest way for them to do that is to simply specify one of the standard charsets.

Table of Contents

Encoding Booleans Bitmaps are a very compact way to encode boolean information, which is often used in protocols. The idea of a bitmap is that each of the bits of an integer type can encode one boolean value— typically with 0 representing false, and 1 representing true.

In general, the int value that has a 1 in bit position i, and a zero in all other bit positions, is just 2i. So bit 5 is represented by 32, bit 12 by , etc. Here are some example mask declarations: Sending and Receiving Data To clear a particular bit, bitwise-AND it with the bitwise complement of the mask for that bit which has ones everywhere except the particular bit, which is zero. GetBytes data ' Begin sending the data to the remote device. BeginSend byteData, 0, byteData.

AsyncState, Socket ' Complete sending the data to the remote device. EndSend ar Console. Shutdown SocketShutdown. Both handler. Close ' Signal the main thread to continue. Net; using System. Sockets; using System. Text; using System. Bind localEndPoint ; listener. WriteLine e. ToString ; if content. EndSend ar ; Console. Both ; handler.The close method terminates the stream, after which further calls to write will throw an exception.

Such a device acts like a router that translates rewrites the addresses and ports in packets as it forwards them. For each Java networking class described in this text, we include only the most important and commonly used methods, omitting those that are deprecated or beyond the use of our target audience.

Alternatively, the server-side protocol processing can be invoked by calling the static method directly passing it the Socket and Logger references. Our example does not include an interrupt call, so the exception will not happen in this application.

The accept method returns a SocketChannel for the incoming connection. Fortunately, sockets provide a way to do this. A stream is simply an ordered sequence of bytes.