473,396 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Client Server application code

1
Hello!I am new in this site and wish to improve my java clent server application code.It's to connect the client to the server through a port and random jokes request by the client to the server be sent to the client.

Thanks
Oct 28 '06 #1
1 4296
horace1
1,510 Expert 1GB
a simple server which waits for message from a client
Expand|Select|Wrap|Line Numbers
  1. // server which waits for messages from a client
  2. import java.io.*;
  3. import java.util.*;
  4. import java.net.*;
  5.  
  6.  
  7. public class TCPserver extends Thread
  8. {
  9.         private ServerSocket   serverSocket;
  10.         ObjectInputStream br=null;
  11.         ObjectOutputStream pw=null;
  12.         int receivePort;                // port to receive  from
  13.         boolean threadRunning = true;   // flag to terminate thread
  14.  
  15.         TCPserver(int receivePort)    // constructor to receive datagrams on port receivePort
  16.     {
  17.                 this.receivePort = receivePort;
  18.     }
  19.  
  20.         // terminate the thread by setting flag
  21.         public void stopTCPserver()
  22.         {
  23.                 threadRunning = false;
  24.         }
  25.  
  26.         public void run()                // thread run method, receives and buffers datagrams
  27.         {
  28.           Socket socket=null;
  29.           try
  30.               {
  31.               while (threadRunning)
  32.               {
  33.                   System.out.println("TCP server starting: IP address " 
  34.                        + InetAddress.getLocalHost().toString() + " port " + receivePort );
  35.                   serverSocket = new ServerSocket(receivePort);
  36.                   socket = serverSocket.accept(); // Wait for client to connect.
  37.                   System.out.println("Client connect from IP address " + socket.getInetAddress()
  38.                                   + " port " + socket.getPort());
  39.                   br  = new ObjectInputStream( ( socket.getInputStream() ) );
  40.                   pw = new ObjectOutputStream( socket.getOutputStream() );
  41.                   while(threadRunning)
  42.                     try
  43.                     {
  44.                       String code = (String) br.readObject();
  45.                       System.out.println( "Server received string: '" + code + "'");
  46.                     }
  47.                     catch (Exception se) {System.err.println("done"); break;}
  48.                   serverSocket.close();
  49.                 }
  50.               }
  51.           catch (Exception se) {System.err.println("run() " + se); }
  52.  
  53.           System.exit(1);                                                 // exit on failure
  54.         }
  55.  
  56.  
  57. public static void main(String args[])
  58. {
  59.         int receivePort=9000;                                 // port to receive datagrams on
  60.         TCPserver frameInput = new TCPserver(receivePort);    // create server to receive messages
  61.         frameInput.start();                                   // and start it
  62.         System.out.println("ready to receive datagrams from socket " + receivePort);
  63.         }
  64.  
  65. }
  66.  
  67.  
a simple client which sends messages entered at keyboard to the server
Expand|Select|Wrap|Line Numbers
  1. // client which sends a message to a server using TCP
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.net.*;
  6.  
  7. public class TCPclient
  8. {
  9.  
  10.         static void sendString(String remoteIPaddress, int remotePort)
  11.         {
  12.         Socket         socket1;        // To server.
  13.         ObjectOutputStream        objOut;        // To write to server.
  14.                 try
  15.                 {
  16.                  socket1 = new Socket( remoteIPaddress, remotePort );   
  17.                  objOut = new ObjectOutputStream( socket1.getOutputStream() );
  18.                  System.out.println("Server contacted OK sending " );
  19.                  while(true)
  20.                    {
  21.                      String s = in.readLine();
  22.                      objOut.writeObject(s);         // send message three times
  23.                    }  // end try
  24.                 }
  25.                 catch (UnknownHostException e) {System.err.println("SendString " +  e); }
  26.                 catch (SocketException se) {System.err.println("SendString " +  se); }
  27.                 catch (IOException e) {System.err.println("SendString " +  e); }
  28.         }
  29.  
  30.   private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  31. public static void main(String args[])
  32. {
  33.    System.out.println("enter message to send to server");
  34.    TCPclient.sendString("127.0.0.1", 9000);
  35. }
  36.  
  37. }
  38.  
1. compile the server and run it
2. compile the client and run it
3. message typed in a client appear at server
Nov 1 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Matt | last post by:
I think this is the basic concept in ASP server-side development. My boss told me web application is NOT client-server application. I argued with him because browser is the client, and the server...
2
by: Microsoft | last post by:
I'm about to start converting my application from a old-style monolith exe (with flat files and limited database support for sharing some of the data) to a layered .NET SQL server version. I have...
18
by: cjl | last post by:
Hey all: I know that it is silly in the age of Google to 'lose' something on the internet, but I recently checked out a project that had implemented a database with a subset of SQL in pure...
4
by: Prince Kumar | last post by:
I joined a company recently and they have a java program which hangs (does nothing) after a while. This is no way consistent. It could succeed quite a few times and can fail a few other times....
6
by: Ken Allen | last post by:
I am relatively new to .Net and C#, but I hav ebeen programing in other languages and done some COM work for a number of years. I am attempting to understand how to map an older program...
10
by: Ben | last post by:
Hi, I made an application in classic asp (reservation of books and video stuffs for students) and want to migrate to asp.net. The user has to chose a date, then pushung on a submit button. The...
22
by: Jordan S. | last post by:
SQL Server will be used as the back-end database to a non trivial client application. In question is the choice of client application: I need to be able to speak intelligently about when one...
13
by: =?Utf-8?B?S2VzdGZpZWxk?= | last post by:
Hi Our company has a .Net web service that, when called via asp.net web pages across our network works 100%! The problem is that when we try and call the web service from a remote machine, one...
2
by: Wimpie van Lingen | last post by:
Hey I have some more questions with regards to Remoting in .NET 2. I'm using TCP with the Binary formatter. My solution consists of 4 projects: - Class Library containing the server classes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.