INDEX ABOUT & TOS PORTAL HELP SEARCH MEMBERS CALENDAR

Create a free forum in seconds.
InvisionFree - Free Forum Hosting
Welcome to beta name Azure Echo v 0.8. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Name:   Password:


 

 [Java] Sockets
Fasga
Posted: Jun 15 2006, 01:06 AM


Newbismal Newb


Group: Members
Posts: 6
Member No.: 22
Joined: 13-June 06



First things first, what is a socket? A socket is like a... Gateway... Between two programs. It can be through the internet, across a network, or just contacting other programs running on the same computer. For this tutorial you should be familiar with IO and Exceptions.

First me create our socket and have it connect to something.
CODE

try
{
Socket s = new Socket("server.servegame.org",43594); //Socket(ip, port)
} catch(Exception e) {System.err.println(e);}


That would open a socket to server.servegame.org on port 43594 NOTE: You have to throw/catch an exception! Now, if the connection is successfully made to the server, then it will continue on with what it was doing. If the connection can not be made, it will execute the catch block.

CODE

try
{
Socket s = new Socket("server.servegame.org",43594);
System.out.println("UP");
} catch(Exception e) {System.out.println("DOWN");}

Basically, that would attempt to connect to the server, if successfull it would print UP, if not, it would stop what its doing and go directly to the catch block printing DOWN.

Now lets write a Client/Server application that will send a message to the server.

CODE

import java.io.*;
import java.net.*;

public class Client
{
public static void main(String[] rr)
{
 try
 {
  Socket s = new Socket("localhost",5000);
  PrintWriter pw = new PrintWriter(s.getOutputStream());
  pw.println("TEST");
  pw.flush();
 } catch (Exception e) {System.err.println(e);}
}
}

CODE

import java.io.*;
import java.net.*;

public class Server
{
public static void main(String[] rr)
{
 try
 {
  ServerSocket ss = new ServerSocket(5000);
  Socket s = ss.accept();
  BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
  while(true)
  {
   System.out.println(br.readLine());
  }
 } catch(Exception e) {System.err.println(e);}

}
}


So, in order to read from a socket, we need to tie it to an InputStreamReader and use the getInputStream method to.. attach... it to the Socket.
For writing we just need to use getOutputStream.

This post has been edited by Miamoto on Jun 15 2006, 03:13 AM
Top
0 User(s) are reading this topic (0 Guests and 0 Anonymous Users)
0 Members:
InvisionFree - Free Forum Hosting
Join the millions that use us for their forum communities. Create your own forum today.

Topic Options



Azure Echo Shoutbox
..::4ZUR3_3C|-|O::..
Hosted for free by InvisionFree* (Terms of Use: Updated 2/10/2010) | Powered by Invision Power Board v1.3 Final © 2003 IPS, Inc.
Page creation time: 0.1518 seconds | Archive
-----------
© 2006-2007 Azure Echo, LLC. All content and images are property of Azure Echo, LLC. unless otherwise specified.
Free Web Counter