Forum Topic: Java Sockets Tutorial

(453 views • 6 replies)

This topic is 1 page long.

<< < > >>
None

TheMick

Reply To Post Reply & Quote

Posted at: 8/24/06 02:55 PM

TheMick NEUTRAL LEVEL 02

Sign-Up: 08/24/06

Posts: 6

Alright, this tutorial is going to be an introduction to sockets in Java. Before reading this, i'd reccommend some experience with basic Java programming.

Alright, let's start a new Java class:

public class yourClass {
public static void main(String[] args) {

Now, the first thing we're going to do is check if the program was given the correct amount of arguments when ran, add this to your code:

if (args.length != 1) {
System.out.println("Usage: Client <address>");
return;
}

Okay, now after the user has ran this program (and entered the host address with it), we want to convert the host to an IP so that Java can connect to it. Add this to your code:

InetAddress addr = null;

try {
addr = InetAddress.getByName(args[0]);
} catch (UnknownHostException e) {
System.err.println("Error, invalid host");
return;
}

Alright, what we just did is take the host (ex. helnet.org) that the user enters into the program and use the getByName function to convert it to an IP so it is possible to connect to later.
Now, our next step is to make the socket, so we can connect to the host. Add this to your code:

Socket sock = null;

try {
sock = new Socket(addr, 21);
} catch (IOException e) {
System.err.println("Error creating socket");
return;
}

Alright. We've just created a socket. That will connect us to the host specified by the user on port 21 (feel free to change the port to whatever you'd like). Now. The next thing we need to do is create an Input Stream so we can read the input of the server. Add this:

InputStream input = null;

try {
input = sock.getInputStream();
} catch (IOException e) {
System.out.println("Error creating input stream");
}

Alright, that's done. Now for the final bit of code. What this part will do is create a buffer reader to read the reply the server gives us when we connect to it. Add this:

BufferedReader readMe = new BufferedReader(new InputStreamReader(input));
String readThis = null;

try {
while((readThis = readMe.readLine()) != null)
System.out.println(readThis);
} catch (IOException e) {
System.err.println("Error reading line");
return;
}

Alright, end the class and main with two more curly braces and the program is done! When you run the program you should get the server's FTP message, for example, when done with helnet.org:

220---------- Welcome to Pure-FTPd [TLS] ----------
220-You are user number 10 of 50 allowed.
220-Local time is now 15:45. Server port: 21.
220-IPv6 connections are also welcome on this server.
220 You will be disconnected after 15 minutes of inactivity.


None

TheMick

Reply To Post Reply & Quote

Posted at: 8/24/06 02:56 PM

TheMick NEUTRAL LEVEL 02

Sign-Up: 08/24/06

Posts: 6

Whoops.
Java: Main


None

SpamBurgeralt

Reply To Post Reply & Quote

Posted at: 8/24/06 02:59 PM

SpamBurgeralt NEUTRAL LEVEL 04

Sign-Up: 07/22/05

Posts: 177

Nice tutorial, well explained.

I'm 12 years old and what is this?


None

Craige

Reply To Post Reply & Quote

Posted at: 8/24/06 03:04 PM

Craige LIGHT LEVEL 08

Sign-Up: 07/17/04

Posts: 3,072

It was an okay tutorial, but It wasn't put together very well, and it could have went into more detail. Also, in your code, the socket connection should have been a instance variable, and you should have had a finally block following the catch blocks that called a method to close the socket connection (or called it in the catch block atleast).


None

amaterasu

Reply To Post Reply & Quote

Posted at: 8/24/06 11:00 PM

amaterasu LIGHT LEVEL 08

Sign-Up: 03/07/04

Posts: 2,073

You should have explained ServerSockets as well, and how to write your own server.

NexusTK Characters: Stegmann Cirucci Ulquiorra Ganju

Do you like chill music? Check out my latest work!

BBS Signature

None

amaterasu

Reply To Post Reply & Quote

Posted at: 8/24/06 11:01 PM

amaterasu LIGHT LEVEL 08

Sign-Up: 03/07/04

Posts: 2,073

okay let me be more specific because what I said sounded naive. I mean a simple server, run from a main method like your client, that maybe just echos back whatever you send to it, or provides info from a really simple data set.

NexusTK Characters: Stegmann Cirucci Ulquiorra Ganju

Do you like chill music? Check out my latest work!

BBS Signature

None

Jordan

Reply To Post Reply & Quote

Posted at: 11/8/06 02:29 AM

Jordan DARK LEVEL 14

Sign-Up: 04/23/06

Posts: 2,833

You didn't import java.net.*; which is required for sockets.


All times are Eastern Standard Time (GMT -5) | Current Time: 12:00 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!