Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Client & Server JAVA Program
#1
Okay so I was looking at a source code written for a java client and server. The client sends information to the server and the server picks up the info.

I took out the main parts of both classes so here they are...

Import the following:
import java.io.*;
import java.net.*;
import java.util.*; // You need this only for the server class


Client side:
PHP Code:
//First create a socket connection to the server we will run:

Socket skt = new Socket(host,port);
//Host is where the ip goes and port is the port. This creates a new socket object

//Now you want to read the info coming from the server and send info out.

BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
//myInput is the object that reads information coming from the server

PrintStream myOutput = new PrintStream(skt.getOutputStream());
//myOutput is the information you send out...

//Here is an example of information being sent to the server with myOutput
myOutput.print("Hello Sir!\n");
//If the server is running it will be able to read this message and reply, store, or do whatever you wish.

//Now lets say the server does indeed send something back... You need to read it.
//Basically the object you created called myInput is the object that reads the information.
//So it's very simple because myInput is already waiting for anything that is sent towards the client.
//Now you just have to make a variable to store the information sent and do whatever you wish for it.
//In this case I'm going to take the info and display it back to the user...

String info myInput.readLine();  //Info stores the information in myInput

System.out.println("Server says " info); //This displays the info to the user 
PHP Code:
Socket skt = new Socket(host,port);

BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
PrintStream myOutput = new PrintStream(skt.getOutputStream());

myOutput.print("Hello Sir!\n");

String info myInput.readLine();  
System.out.println("Server says " info); 
Server side:
PHP Code:
//For the server you must create a server socket and communicate to the client.
ServerSocket myServerSocket = new ServerSocket(port);

//Then create a socket so that it accepts the incoming connection to myServerSocket.
Socket skt myServerSocket.accept();


//Basically the reading and writing is the same as in the client side. Here is the code used but if you //want to know how it works refer to the client class.
BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
PrintStream myOutput = new PrintStream(skt.getOutputStream());

//You want to read the info from the client side so create a variable.
String infoinput.next(); //info stores the information from the input stream
System.out.println("Info was " info); //Simply displays info

//Then send information back to the client... To do this correctly use an if statement.
if (info != null) {     //This is saying if info does not equal null then....
myOutput.print("got it"); //Sends this to the client
}

//After the server is done running try to close the connection
skt.close(); 
System.out.println("Server is exiting"); 
PHP Code:
ServerSocket myServerSocket = new ServerSocket(port);


Socket skt myServerSocket.accept();
Scanner input = new Scanner(System.in);

BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
PrintStream myOutput = new PrintStream(skt.getOutputStream());

String info myInput.readLine();
System.out.println("Info was " info); 

if (
info != null) {   
myOutput.print("got it"); 
}

skt.close(); 
System.out.println("Server is exiting"); 
Please remember that you should try to catch exceptions or tell the method to throw the exception.



This is easy as hell and if you understand this you can understand the concept between internet communication. Mostly everything from surfing the web to gaming online has a sort of system like this. Also if you can fully understand this then you know what other naughty things you can do with it Smile.

To sum it up the client creates a socket that has an input stream and output stream that sends and receives information from the server. The server waits for information to be sent and then decides on the reply that you have specified.

If you have any questions just reply
#2
cool story bro, literally.
just imagine... all the possibilities, THEY ARE ENDLESS!
#3
is java worth leaning cuz i want to learn a programming lang Tongue
~The lonely Stoner seems to free his mind at night~
#4
Dre@m$ Wrote:is java worth leaning cuz i want to learn a programming lang Tongue
It's Great, but i would really recommend C#, it's a lot more Powerful on windows, and looks very much alike Java, so you can switch easily when u want to.
[Image: bxj6gaq99c9hyvinfkvn.jpg]
[Image: w0l5y0.png]

Users browsing this thread: 1 Guest(s)