What's Here?
- Members: 244,258
- Replies: 693,056
- Topics: 113,147
- Snippets: 3,863
- Tutorials: 935
- Total Online: 1,288
- Members: 99
- Guests: 1,189
|
A simple Client/Server demo program.
The server waits for connection and starts a thread when there is a connection request from a client. The server receives a String from the client and returns it to the client in uppercase.
|
Submitted By: pbl
|
|
Rating:
 
|
|
Views: 6,927 |
Language: Java
|
|
Last Modified: April 28, 2008 |
Instructions: The client connects to the server specifying "localhost". That means the server and the client are on the same system.
The port 1500 is used for communication. |
Snippet
//The server code Server.java:
import java.io.*;
import java.net.*;
/**
* This is to help people to write Client server application
* I tried to make it as simple as possible... the client connect to the server
* the client send a String to the server the server returns it in UPPERCASE thats all
*/
public class Server {
// the socket used by the server
// server constructor
Server(int port) {
/* create socket server and wait for connection requests */
try
{
System. out. println("Server waiting for client on port " + serverSocket. getLocalPort());
while(true)
{
Socket socket = serverSocket. accept(); // accept connection
System. out. println("New client asked for a connection");
TcpThread t = new TcpThread(socket); // make a thread of it
System. out. println("Starting a thread for a new Client");
t.start();
}
}
System. out. println("Exception on new ServerSocket: " + e );
}
}
// you must "run" server to have the server run as a console application
public static void main (String[] arg ) {
// start server on port 1500
new Server(1500);
}
/** One instance of this thread will run for each client */
class TcpThread extends Thread {
// the socket where to listen/talk
this.socket = socket;
}
public void run() {
/* Creating both Data Stream */
System. out. println("Thread trying to create Object Input/Output Streams");
try
{
// create output first
Soutput.flush();
}
System. out. println("Exception creating new Input/output Streams: " + e );
return;
}
System. out. println("Thread waiting for a String from the Client");
// read a String (which is an object)
try {
str = str.toUpperCase();
Soutput.writeObject(str);
Soutput.flush();
}
System. out. println("Exception reading/writing Streams: " + e );
return;
}
// will surely not happen with a String
}
finally {
try {
Soutput.close();
Sinput.close();
}
}
}
}
}
}
//The client code Client.java:
import java.net.*;
import java.io.*;
public class Client {
// Constructor connection receiving a socket number
Client(int port) {
// we use "localhost" as host name, the server is on the same machine
// but you can put the "real" server name or IP address
try {
socket = new Socket("localhost", port );
}
System. out. println("Error connectiong to server:" + e );
return;
}
System. out. println("Connection accepted " +
socket.getInetAddress() + ":" +
socket.getPort());
/* Creating both Data Stream */
try
{
}
System. out. println("Exception creating new Input/output Streams: " + e );
return;
}
// now that I have my connection
String test = "aBcDeFgHiJkLmNoPqRsTuVwXyZ";
// send the string to the server
System. out. println("Client sending \"" + test + "\" to serveur");
try {
Soutput.writeObject(test);
Soutput.flush();
}
System. out. println("Error writting to the socket: " + e );
return;
}
// read back the answer from the server
try {
response = (String) Sinput. readObject();
System. out. println("Read back from server: " + response );
}
System. out. println("Problem reading back from server: " + e );
}
try{
Sinput.close();
Soutput.close();
}
}
public static void main (String[] arg ) {
new Client(1500);
}
}
Copy & Paste
|
|
|
Be Social
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|