Head First Java-15 th chapter

Shiva Mohan

Ranch Hand

Posts: 486

posted 11 years ago

Hi All,

Just now atarted reading "Make a Connetion"(chapter 15) from head first java.Until this time,i am executing all my java progams in netbeans.Can i execute client and server code here itself.What they mean by socket and what is port?I thought Socket class is used to make a connection to the server.please,Give more details about it.

Robert Hill

Ranch Hand

Posts: 94

posted 11 years ago

Before starting into the fun and interesting world of network programing, you need to learn the basics.

A socket is an abstraction. You can not literally connect to a computer on the internet. It is not very practical to hardwire your machine to one 1000 miles away.

A socket approximates this, by initially sending basic information about you to a server(IP address, port, ect), and the server keeps this data during the connection(at least for TCP sessions),

A port is analogous to an operating system process ID. there is nothing magical about them, it is just a number(short) that the OS assigns to network programs. This way, the OS knows which program to forward the incoming data to. Without them, only one program could connect to the internet/network. Ports from 0 to 1023 are called well-known ports and should only be used to implement established protocols(ie HTTP, FTP, echo, ect). The are controlled by the Intrernet Assigned Numbers Authority. Ports from 1024-49000ish(I forget the exact number) are registered ports, they are not controlled by the IANA, but different port ranges are described for different uses. The rest are uncontrolled and basically free-for-alls.

A socket normally connects to a server, but servers also use them to communicate back to clients. They are also used in peer-to-peer.

"Can i execute client and server code here itself"

Not sure what you mean.

IMO, you should learn about basic underpinnings of network protocols first. How and what TCP/IP and UDP/IP do. What IP addresses are and how they are resolved, submasks, ect. Also, little-endian vs big-endian and how to over come byte-order differences; and reading and writing bytes are especially important if your server or client will communicate with lots of different OS's and are not necessarily written in Java. I suppose you don't really need to know this stuff to use Java implementations, but knowing it helps you use the java.net API more effectively and you will know when and why are you going in the wrong direction.

So read up on basic networking, and then dive into Socket programming. If you want other references other then HF Java, try the tutorials at Sun's website. A reasonably decent beginers book is "TCP/IP Sockets in Java" by Calvert/Donahoo. It is less then $20 also.

Shiva Mohan

Ranch Hand

Posts: 486

posted 11 years ago

Robert thankyou very much for your kind information.Ya...first i will go learn the basics of network programming.