What is the actual operation of Java Socket communication?
Today, I will talk to you about the actual operation of Java Socket communication, many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Java Socket communication has a lot of things we need to pay attention to when we actually use it. So first of all, let's look at how Java Socket communication is prepared and how to establish a connection in practice. I hope you all get something.
First, prepare:
Server: JDK1.5 (no need to introduce this, right? )
Server IDE:eclipse (its home page)
Client: FLEX 3 (Adobe ®Flex ®3) is an efficient open source framework for building and maintaining expressive Web applications deployed consistently on all major browsers, desktops, and operating systems. )
Client IDE:Flex Builder 3 (its official summary page)
Second, the encounter of fate (establishing a connection):
Since it is a communication, of course, the step is to establish a connection. Let's start our connection with the easiest way to look at the code.
Server code: Jserver1.java
Java code
Import java.net.*
Import java.io.*
Public class Jserver1 {
Public static void main (String [] args) {
ServerSocket server=null
Try {
Server=new ServerSocket / / create a server socket object on port 719
System.out.println ("server waiting")
While (true) {
System.out.println ("waiting for the client")
Socket newSocket=server.accept (); / / the server set up by the client to us
If a connection request is sent by a connector, it will be executed down.
System.out.println ("connected to client")
}
} catch (IOException ie)
{
System.out.println (ie)
} finally {
Try
{
If (server socket null) server.close (); / / closes the server socket. Why do you use it?
Finally keyword to do this? The master in HOHO thinking in java has already
To make it clear, no matter what happens, our server-side sockets are guaranteed to be turned off.
} catch (IOException ie) {}
}
}
After reading the above, do you have any further understanding of the actual operation of Java Socket communication? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.