article

Intro To Network Programming: Sockets

Email
Submitted on: 1/5/2015 3:39:00 PM
By: princennamdi (from psc cd)  
Level: Intermediate
User Rating: By 5 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 2547
 
     Just a basic intro into socket programming..

 
				im sure you know what an IP address is, and what a port is....
if u havent, check wikipedia.org
a socket is the combination of an IP address and a port.......
eg...50.50.50.50 : 21
a server is anything that provides a service or services...
and each of those services can be accessed through the respective port...
eg
say if my server ip address is 50.50.50.50, 
and i run FTP and HTTP and BLAH on port 21, 80, 9090 respectively.....
if john wants to access FTP services on my server, 
he has to use  50.50.50.50:21 to access it
if he wants HTTP services on my server, he has to use  50.50.50.50:80
 to access it
and if he wants to access BLAH( my special service ),
 he has to use  50.50.50.50:9090 to access it.....
this tutorial will show you how to open a BLAH or whateva service on your system
, so anyone can connect and access your program/service......
Note....you can write custom socket services that can run commands,
 much like netcat in fact, every program that requires a network, 
uses socket...eg limewire
first, since we are networking, we need to import the network class

import java.net.*;

then we need to import the I/O class....ie for sending and recieving data through the socket
NOTE....i wont send or recieve in this tutorial tho...

import java.io.*;

then we need to import the Exception class....for handling errors....
note the exception class is still under the java.io.*........
but i specifically imported it again to show you
 the exact class is java.io.IOException; 

import java.io.IOException*;

now lets write the obvious

class server{
 public static void main(String[] args) throws IOException{
System.out.println("Starting socket......Socket started on port 9090")
ServerSocket s=new ServerSocket(9090); //i want the service to run on port 9090
System.out.println("Waiting for connections.....")
Socket S=s.accept();//note program pauses here until connection is made 
System.out.println("Connection Established.....")
//if you want to close the socket use S.close()
//if you close the socket you can still reopenit again at port 9090,
//but if you dont close, you have to use another port number...for now, dont close
//, just be using different port numbers
}
}

well thats it...compile and run
you now have a service running on port whatever you choose...
now lets test it.....while the
program is still running, open up a command prompt and type in.

telnet localhost 9090

thats it....
we just wrote the server side of the socket.....in the next tutorial, ill show 
you how to send and recieve input through the socket( using Buffered Streams )
and then ill show you how to build a simple chat server, using sockets...
note: i stripped down unnecessary declarations and stuffs to make the tutorial easy......
any Questions, email me


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.