473,749 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

client and server program in JSP. (moved to java)

58 New Member
Hi all,
I am a newbie to JSP. I had a program in client and server by using java.

But i want the same program in JSP. Please help me.


server:
Expand|Select|Wrap|Line Numbers
  1.      // TCP server which waits for messages from a client - non threaded
  2. import java.io.*;
  3. import java.util.*;
  4. import java.net.*;
  5.  
  6. public class TCPserver1 
  7. {
  8. public static void main(String args[])
  9. {
  10.         int receivePort=9000;             // port to receive TCP connections
  11.          Socket socket=null;
  12.          while(true)                      // when client closed start again
  13.             try
  14.                 {
  15.                   System.out.println("TCP server starting: IP address " 
  16.                        + InetAddress.getLocalHost().toString() + " port " + receivePort );
  17.                   ServerSocket serverSocket = new ServerSocket(receivePort);
  18.                   socket = serverSocket.accept();    // Wait for client to connect.
  19.                   System.out.println("Client connect from IP address " + socket.getInetAddress()
  20.                                   + " port " + socket.getPort());
  21.                   ObjectInputStream br  = new ObjectInputStream( ( socket.getInputStream() ) );
  22.                   ObjectOutputStream pw = new ObjectOutputStream( socket.getOutputStream() );
  23.                   while(true)                       // receiving messages from client
  24.                     try
  25.                     {
  26.                       String code = (String) br.readObject();
  27.                       System.out.println( "Server received string: '" + code + "'");
  28.                     }
  29.                     catch (Exception se) {System.err.println("closing connection"); break;}
  30.                   serverSocket.close();
  31.                 }
  32.             catch (Exception se) {System.err.println("run() " + se); }
  33.         }
  34. }
  35.  
  36.  
client
Expand|Select|Wrap|Line Numbers
  1. // TCP client which sends a message to a server 
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.net.*;
  6.  
  7. public class TCPclient
  8. {
  9. private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.  
  12. public static void main(String args[])
  13. {
  14.    System.out.println("enter message to send to server");
  15.    String remoteIPaddress="127.0.0.1";
  16.    int remotePort=9000;
  17.    try
  18.        {
  19.         Socket socket1 = new Socket( remoteIPaddress, remotePort );   
  20.         ObjectOutputStream objOut = new ObjectOutputStream( socket1.getOutputStream() );
  21.         System.out.println("Server contacted OK , enter text to send " );
  22.         while(true)
  23.             {
  24.              String s = in.readLine();
  25.              objOut.writeObject(s);         // send message
  26.             }  
  27.         }
  28.     catch (IOException e) {System.err.println("TCP client error " +  e); }
  29.  }
  30.  
  31. }
  32.  
  33.  

-Raju
Dec 12 '06 #1
4 6510
b1randon
171 Recognized Expert New Member
Hi all,
I am a newbie to JSP. I had a program in client and server by using java.

But i want the same program in JSP. Please help me.


server:
Expand|Select|Wrap|Line Numbers
  1.      // TCP server which waits for messages from a client - non threaded
  2. import java.io.*;
  3. import java.util.*;
  4. import java.net.*;
  5.  
  6. public class TCPserver1 
  7. {
  8. public static void main(String args[])
  9. {
  10.         int receivePort=9000;             // port to receive TCP connections
  11.          Socket socket=null;
  12.          while(true)                      // when client closed start again
  13.             try
  14.                 {
  15.                   System.out.println("TCP server starting: IP address " 
  16.                        + InetAddress.getLocalHost().toString() + " port " + receivePort );
  17.                   ServerSocket serverSocket = new ServerSocket(receivePort);
  18.                   socket = serverSocket.accept();    // Wait for client to connect.
  19.                   System.out.println("Client connect from IP address " + socket.getInetAddress()
  20.                                   + " port " + socket.getPort());
  21.                   ObjectInputStream br  = new ObjectInputStream( ( socket.getInputStream() ) );
  22.                   ObjectOutputStream pw = new ObjectOutputStream( socket.getOutputStream() );
  23.                   while(true)                       // receiving messages from client
  24.                     try
  25.                     {
  26.                       String code = (String) br.readObject();
  27.                       System.out.println( "Server received string: '" + code + "'");
  28.                     }
  29.                     catch (Exception se) {System.err.println("closing connection"); break;}
  30.                   serverSocket.close();
  31.                 }
  32.             catch (Exception se) {System.err.println("run() " + se); }
  33.         }
  34. }
  35.  
  36.  
client
Expand|Select|Wrap|Line Numbers
  1. // TCP client which sends a message to a server 
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.net.*;
  6.  
  7. public class TCPclient
  8. {
  9. private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.  
  12. public static void main(String args[])
  13. {
  14.    System.out.println("enter message to send to server");
  15.    String remoteIPaddress="127.0.0.1";
  16.    int remotePort=9000;
  17.    try
  18.        {
  19.         Socket socket1 = new Socket( remoteIPaddress, remotePort );   
  20.         ObjectOutputStream objOut = new ObjectOutputStream( socket1.getOutputStream() );
  21.         System.out.println("Server contacted OK , enter text to send " );
  22.         while(true)
  23.             {
  24.              String s = in.readLine();
  25.              objOut.writeObject(s);         // send message
  26.             }  
  27.         }
  28.     catch (IOException e) {System.err.println("TCP client error " +  e); }
  29.  }
  30.  
  31. }
  32.  
  33.  

-Raju
Raju, JavaScript and JSP are very different. You'll need to post in another thread if you're looking for good replies. Try reading the JS vs. JSP post at the top of the category. I hope you have better luck in another thread.
Dec 12 '06 #2
maverick19
25 New Member
use the page directive to import the java code to your jsp
then you can easily call the method in the jsp its simple java there
Expand|Select|Wrap|Line Numbers
  1. <%@ page import="mypackage.*" %> 
  2.  
  3. and somewhere in the jsp 
  4. <%
  5. Myclass check = new Myclass()
  6. check.mymethod();
  7. %>
  8.  
Dec 13 '06 #3
r035198x
13,262 MVP
Hi all,
I am a newbie to JSP. I had a program in client and server by using java.

But i want the same program in JSP. Please help me.


server:
Expand|Select|Wrap|Line Numbers
  1. // TCP server which waits for messages from a client - non threaded
  2. import java.io.*;
  3. import java.util.*;
  4. import java.net.*;
  5.  
  6. public class TCPserver1 
  7. {
  8. public static void main(String args[])
  9. {
  10. int receivePort=9000; // port to receive TCP connections
  11. Socket socket=null;
  12. while(true) // when client closed start again
  13. try
  14. {
  15. System.out.println("TCP server starting: IP address " 
  16. + InetAddress.getLocalHost().toString() + " port " + receivePort );
  17. ServerSocket serverSocket = new ServerSocket(receivePort);
  18. socket = serverSocket.accept(); // Wait for client to connect.
  19. System.out.println("Client connect from IP address " + socket.getInetAddress()
  20. + " port " + socket.getPort());
  21. ObjectInputStream br = new ObjectInputStream( ( socket.getInputStream() ) );
  22. ObjectOutputStream pw = new ObjectOutputStream( socket.getOutputStream() );
  23. while(true) // receiving messages from client
  24. try
  25. {
  26. String code = (String) br.readObject();
  27. System.out.println( "Server received string: '" + code + "'");
  28. }
  29. catch (Exception se) {System.err.println("closing connection"); break;}
  30. serverSocket.close();
  31. }
  32. catch (Exception se) {System.err.println("run() " + se); }
  33. }
  34. }
  35.  
  36.  
client
Expand|Select|Wrap|Line Numbers
  1. // TCP client which sends a message to a server 
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.net.*;
  6.  
  7. public class TCPclient
  8. {
  9. private final static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.  
  12. public static void main(String args[])
  13. {
  14. System.out.println("enter message to send to server");
  15. String remoteIPaddress="127.0.0.1";
  16. int remotePort=9000;
  17. try
  18. {
  19. Socket socket1 = new Socket( remoteIPaddress, remotePort ); 
  20. ObjectOutputStream objOut = new ObjectOutputStream( socket1.getOutputStream() );
  21. System.out.println("Server contacted OK , enter text to send " );
  22. while(true)
  23. {
  24. String s = in.readLine();
  25. objOut.writeObject(s); // send message
  26. }
  27. catch (IOException e) {System.err.println("TCP client error " + e); }
  28. }
  29.  
  30. }
  31.  
  32.  

-Raju
You could use a jsp for the client actions and a servlet for the server actions
Dec 13 '06 #4
rajbala
58 New Member
yes it's working thank you to all for suggestion.
-raju
Dec 14 '06 #5

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

Similar topics

7
3372
by: CT | last post by:
Hi, This might seem like a basic question but I have some doubts, please humour me. I have a client-server application using java where each client on each machine needs to directly communicate directly with the database. Do I need a separate db2 connect on each such machine. Please advice.
4
6593
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. There is no consistency when it fails. Could anyone here shed some light on how to debug/resolve the issue. I guess IBM looked at the issue and were not able to pinpoint where the issue is. When the program hangs and when force the DB2...
3
6914
by: Roy Souther | last post by:
Trying to get the unixODBC to work connecting to an IBM DB2 UDB V8.1. The DB2 server is running on Red Hat 8 and is tested and confirmed to be serving connections to Windows 98 clients using the Accpac 5.1 client program (I would like to find an OpenSource generic ODBC program for Wiindows that can do simple SQL). An Accpac database was installed and the Win32 ODBC connection is working. Trying to connect a Linux client via unixODBC I...
5
13123
by: Ankur | last post by:
Hi Folks, I am new for this group. I want to clarify one thing what's a basic difference between Client Side Java Script and Server Side Java Script. how we can differentiate it. Why we called this kind of script "Server Side Java Script". I am in confusion because according to lots of web developer Java Script is a client side scripting language. We can not say it Server Side Java Script.
5
2801
by: ganeshp | last post by:
Hi , In Java using socket programming is it possible to have a server program on windows that services a client program on linux? I tried the code in the below given link: http://williams.comp.ncat.edu/Networks/JavaSocketExample.htm This does not work when the client program is on a Linux machine. Error: Error Connection refused
3
1219
by: Peter Webb | last post by:
I need to make a reasonably big decision, and I need some advice. I have used C# to make some pretty nice and potentially very educational toys/animation. One does n body orbit simulations, another does gas molecules interacting according to the ideal gas law, and another generates hypercycloids. I have loaded them up with eye candy, and they produce terrific graphics - I would expect that many children/teens would use them as pattern...
2
1809
by: JosAH | last post by:
I run my Java programs on a small Linux laptop and today I installed it again on a MS Vista laptop.My linux laptop ran quite a bit faster than my Vista laptop and I decided to find out why. After a bit of digging I found out that the 'server' hotspot comes with the Linux JRE distribution but it isn't there in the MS Windows distribution; it comes with the JDK though. This is what I'm talking about: go to your JAVA_HOME location; that...
3
11680
by: TsanChung | last post by:
I want to make a java TCP socket client to communicate with a TCP server socket on linux. Are there some sample C unix server and java client socket programs available? The Richard Stevens' "Unix network programming" book described a TCP server (tcpcliserv04.c) and client (tcpcli04.c). I compiled and executed them successfully as follows: $ tcpcliserv04 &
7
4690
by: Anil | last post by:
I have a Javascript program which runs in the browser and has functions work(), and stop(). It listens to commands from the server to work() and can be interrupted by the server to stop(). I am using XmlHttpRequest to talk to the server. So I use http GET to send a command "ready" to the server, which replies at some point in time by sending "work" which is invoked by the callback. As per my understanding, the browser client is single...
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.