Sockets

The System.Net.Sockets namespace contains classes used to create applications that work directly with the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP). These classes build on the native socket classes discussed in the Programming with Native Sockets section later in this chapter.

Creating a TCP Client

To illustrate the use of the TCP client classes, we’ll read the index page from the Microsoft Web site. Here is a Java example using the Socket class:

import java.net.*; import java.io.*; public class SocketClient { public SocketClient() throws IOException, UnknownHostException { Socket x_socket = new Socket("www.microsoft.com", 80); PrintWriter x_writer = new PrintWriter(x_socket.getOutputStream(), true); x_writer.println("GET ...

Get C# for Java Developers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.