answersLogoWhite

0

Apologies in advance for the lengthy example. Below is an example of a client-server pair sending each other a message. This shows Java's TCP implementation of Server/ServerSocket classes because they're very simple and easy to use.

A few things to be aware of when testing these classes out:

# You can start one of these classes with TCPServer.start() or TCPClient.start().

# Always start the TCPServer class before the TCPClient class. If you don't, it's possible that the TCPClient will throw an exception and exit quietly, while the TCPServer class waits forever for a client to connect. # The current setup will connect on 127.0.0.1, which is your own computer. This will work even if you have no network/internet connection. If you wish to connect to a remote computer, change the TCPClient.HOST value to the IP address (or hostname) of the computer that will be running the server. // Code to run both parts of the program from the same computer.

// (This will make for some jumbled output)

class Main {

public static void main(String[] args) {

new TCPServer().start();

new TCPClient().start();

}

}

class TCPClient extends Thread {

// Connection properties

private static final String HOST = "127.0.0.1";

private static final int PORT = 56565;

public void run() {

// Socket class used for TCP connections

Socket sock = null;

// I/O components

BufferedReader input = null;

BufferedWriter output = null;

try {

// Connect our socket to the server.

sock = new Socket(HOST, PORT);

// Use a BufferedReader to read data from the server.

input = new BufferedReader(new InputStreamReader(sock.getInputStream()));

// Use a BufferedWriter to send data to the server.

output = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));

// Send some data to the server.

String toServer = "Are you there, Server?";

System.out.println("ToServer: " + toServer);

output.write(toServer);

output.newLine();

output.flush();

// Wait for a response from the server and display it.

String fromServer = input.readLine();

System.out.println("FromServer: " + fromServer);

} catch (final IOException ex) {

} finally {

// Do our best to ensure a clean close procedure.

// Closing the socket will also close input and output.

try {

if (sock != null) {

sock.close();

}

} catch (final IOException ex) {

}

}

}

}

class TCPServer extends Thread {

// Connection properties

private static final int PORT = 56565;

public void run() {

// ServerSocket class used to accept TCP connections

ServerSocket server = null;

Socket sock = null;

// I/O components

BufferedReader input = null;

BufferedWriter output = null;

try {

// Create our server on the given port.

server = new ServerSocket(PORT);

// Wait for a client to connect to us.

sock = server.accept();

// Use a BufferedReader to read data from the client.

input = new BufferedReader(new InputStreamReader(sock.getInputStream()));

// Use a BufferedWriter to send data to the client.

output = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));

// Wait for the client to talk to us.

String fromClient = input.readLine();

System.out.println("FromClient: " + fromClient);

// Send them a response.

String toClient = "Yes, I'm here, Client.";

System.out.println("ToClient: " + toClient);

output.write(toClient);

output.newLine();

output.flush();

} catch (final IOException ex) {

} finally {

// Do our best to ensure a clean close procedure.

// Closing the socket will also close input and output.

try {

if (sock != null) {

sock.close();

}

} catch (final IOException ex) {

}

// Close the server socket, as well.

try {

if (server != null) {

server.close();

}

} catch (final IOException ex) {

}

}

}

}

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

Why must the server program be executed before the client program?

A server program must be executed before the client program because once a client is run, it will attempt to initiate a connection with the server. If the server is down, then the client will not be able to make a connection.


How do you program?

Programming involves designing a program mock-up, documenting the program, and coding it into a development environment. Most of all, programming involves making sure that the client is satisfied with all the capabilities of the program.


What are the differences between client-side programming and server-side programming?

All of us (or most of us) would have started programming in Java with the ever famous "Hello World!" program. If you can recollect, we saved this file with a .java extension and later compiled the program using javac and then executed the class file with java. Apart from introducing you to the language basics, the point to be noted about this program is that - "It is a client side program". This means that you write, compile and also execute the program on a client machine (e.g. Your PC). No doubt, this is the easiest and fastest way to write, compile and execute programs. But, it has little practical significance when it comes to real world programming.


What is a data base connection?

When we create a website or a project in programming language we need to use a database for storing the data . A database connection used to establish a connection between the server and the client. For this jdbc jar is added in the project in case of java .


What is a compiled program that runs on the client?

The program that runs on the client computer is the client program. Web-browser is a prominent example for client program.


What is a base connection?

When we create a website or a project in programming language we need to use a database for storing the data . A database connection used to establish a connection between the server and the client. For this jdbc jar is added in the project in case of java .


What is client program?

As far as web programming is concerned, client side programming is code that runs in the web browser, rather than the web server. JavaScript is an example of client side scripting because the code is sent to the browser, at which point it is executed. PHP is an example of server side scripting because the code is executed on the server, and the resulting code is sent to the browser and displayed.


Does a server program request and receive services from a client program?

No. A server program receives and processes requests from a client program.


Internet programming differ from other programming?

Internet programming is generally a client/server based programming where there will be servers for handling requests and clients for sending request these may be on different computers but the other programing may or may not be client server based which is on the same PC


Which program is always running in client server?

Server program is always running in client server.


What type of program is the free downloadable BitLord?

The free downloadable program BitLord is an open source software client for BitTorrent. It is easy to use and allows someone to participate in file sharing.


With the aid of diagrams describe the basic steps in client programs data to a server and receiving a response from the server?

The type of connection varies greatly depending on the client/server. We can not provide any information without knowing the type of software you are refering to. It is likely that the client/server to uses TCP as its network protocol. Research TCP to understand the basics of how computers usually communicate over an IP network. The alternitive to TCP is UDP, which is a less reliable protocol. In a nutshell (without diagrams, sorry), most client/server connections just use TCP, then some sort of data. Data is usually sent only on one port. The client sends a SYN (syncronize) to the server which replies with an ACK/SYN (acknowledge/syncronize), to which the client replies with an ACK (acknowledge). This opens a connection from the client to the server where data is sent or requested in a format special to the type of program being used. The best way, although somewhat more advanced, is to download a packet capturing program such as the open source program Wireshark from www.wireshark.org. That will allow you to capture traffic to and from the client/server.