| Start |
|---|
Basic Client and Server Communication using a Socket Server: Start, and wait for incoming connectionsServer: Handling clientsWe'll handle each client in a separate thread so multiple clients could interact with the server at the same time. This technique works fine as long as the number of clients is low (<< 1000 clients, depending on the OS architecture and the expected load of each thread). Client: Connect to the server and send a messageClosing Sockets and Handling ExceptionsThe above examples left out some things to make them easier to read. Sockets handle I/O (Input/Output) operations that depend on a variety of external factors. For example what if the other side suddenly disconnects? What if there are network error? These things are beyond our control. This is why many socket operations might throw exceptions, especially `IOException`. A more complete code for the client would therefore be something like this: Basic Server and Client - complete examplesServer: Client: |
| Basic Client/Server Communication using UDP (Datagram) Client.java In this case, we pass in the address of the server, via an argument ( Server.java On the server-side, declare a DatagramSocket on the same port which we sent our message to (4160) and wait for a response. |
Loading TrustStore and KeyStore from InputStream Intiating a KeyStore works the same, except replace any word |
Socket example - reading a web page using a simple socket You should get a response that starts with Note the This example is meant as a simple demonstration of connecting to an existing resource using a socket, it's not a practical way of accessing web pages. If you need to access a web page using Java, it's best to use an existing HTTP client library such as Apache's HTTP Client (opens new window) or Google's HTTP Client (opens new window) |
| Multicasting Multicasting is a type of Datagram Socket. Unlike regular Datagrams, Multicasting doesn't handle each client individually instead it sends it out to one IP Address and all subscribed clients will get the message. Example code for a server side: Example code for a client side: Code for running the Server: Code for running a Client: Run the Client First: The Client must subscribe to the IP before it can start receiving any packets. If you start the server and call the |
| Temporarily disable SSL verification (for testing purposes) Sometimes in a development or testing environment, the SSL certificate chain might not have been fully established (yet). To continue developing and testing, you can turn off SSL verification programmatically by installing an "all-trusting" trust manager: |
| Downloading a file using Channel If the file already exists, it will be overwritten! Notes
Syntax
|
Read Article
https://develop.horje.com/learn/1434/reference