Software School Projects | Academic Students Projects | Source Codes | Tablets header
Please use our contact us form or send email to Support@srishtis.com.

Example
Client.java

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

public class Client {
public static void main(String[] ar) {
int serverPort = 6666; // make sure you give the port number on which the server is listening.
String address = "127.0.0.1"; // this is the IP address of the server program's computer.
// the address given here means "the same computer as the client".

try {
InetAddress ipAddress = InetAddress.getByName(address);
// create an object that represents the above IP address. System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?");
Socket socket = new Socket(ipAddress, serverPort);
// create a socket with the server's IP address and server's port. System.out.println("Yes! I just got hold of the program.");
// Get the input and output streams of the socket, so that you can receive and send data to the client.
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
// Just converting them to different streams, so that string handling becomes easier.
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
// Create a stream to read from the keyboard.
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
System.out.println("Type in something and press enter. Will send it to the server and tell ya what it thinks.");
System.out.println();
while(true) {
line = keyboard.readLine(); // wait for the user to type in something and press enter.
System.out.println("Sending this line to the server...");
out.writeUTF(line); // send the above line to the server.
out.flush(); // flush the stream to ensure that the data reaches the other end.
line = in.readUTF(); // wait for the server to send a line of text.
System.out.println("The server was very polite. It sent me this : " + line);
System.out.println("Looks like the server is pleased with us. Go ahead and enter more lines.");
System.out.println();
}
} catch(Exception x) {
x.printStackTrace();
}
}
}
Job or extra money for students

Search Engine Rank of your blog or websites