Fusun,
The application should always be listening for connections.
Your serverSocket.accept() method call should be in a while loop like this:

  // server infinite loop
import java.net.*;
import java.io.*;

public class tcpServer {

     public static void main(String args[]) {

         int port;
         ServerSocket server_socket;
         BufferedReader input;

         try {
             port = Integer.parseInt(args[0]);
         }
         catch (Exception e) {
             System.out.println("port = 1500 (default)");
             port = 1500;
         }

         try {

             server_socket = new ServerSocket(port);
             System.out.println("Server waiting for client on port " +
                                server_socket.getLocalPort());

             while(true) {
                 Socket socket = server_socket.accept();
                 System.out.println("New connection accepted " +
                                    socket.getInetAddress() +
                                    ":" + socket.getPort());
                 input = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
                 // print received data
                 try {
                     while(true) {
                         String message = input.readLine();
                         if (message==null) break;
                         System.out.println(message);
                     }
                 }
                 catch (IOException e) {
                     System.out.println(e);
                 }

                 // connection closed by client
                 try {
                     socket.close();
                     System.out.println("Connection closed by client");
                 }
                 catch (IOException e) {
                     System.out.println(e);
                 }

             }

The accept method will block until a connection is made to the server
socket port.  Once a socket is established, you need to do whatever you
need to do to process the data going into or out of the socket by getting
the input or output streams of the socket and reading or writing from it.

To read more about socket programming, go to: http://pont.net/socket/java/

-Richard

At 08:30 AM 9/24/01 +0300, you wrote:
>     Assume that an application waits for connection :
>         ServerSocket s = new ServerSocket(8088);
>         Socket clientSocket = serverSocket.accept();
>
>     A Servlet want to make connection:
>             PrintWriter pw = response.getWriter();
>             Socket clientSocket = new Socket(hostAddress, 8088);
>             pw.println("<HTML>");
>             pw.println("<BODY>");
>             pw.println("hello!");
>             pw.println("</BODY>");
>             pw.println("</HTML>");
>
>
>
>      Nothing more is in doGet or doPost method.
>
>     I call servlet from browser, servlet prints hello and stops. However,
>application still waiting connection.
>
>     Thanks,
>
>
>
>
>
>
>----- Original Message -----
>From: "Christopher K. St. John" <[EMAIL PROTECTED]>
>To: <[EMAIL PROTECTED]>
>Sent: Friday, September 21, 2001 9:11 PM
>Subject: Re: Socket connection is servlet
>
>
> > Fusun CITAK wrote:
> > >
> > > I must connect with this application from a servlet.
> > > Is that possible?
> > >
> >
> >  Yes.
> >
> >
> > > What is wrong?
> > >
> >
> >  Without details, it's a little hard to tell. If
> > you can't post your code, at least post any error
> > messages you're getting. Or something.
> >
> >
> >
> > --
> > Christopher St. John [EMAIL PROTECTED]
> > DistribuTopia http://www.distributopia.com
> >
> >
>___________________________________________________________________________
> > To unsubscribe, send email to [EMAIL PROTECTED] and include in the
>body
> > of the message "signoff SERVLET-INTEREST".
> >
> > Archives: http://archives.java.sun.com/archives/servlet-interest.html
> > Resources: http://java.sun.com/products/servlet/external-resources.html
> > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
> >
>
>___________________________________________________________________________
>To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
>of the message "signoff SERVLET-INTEREST".
>
>Archives: http://archives.java.sun.com/archives/servlet-interest.html
>Resources: http://java.sun.com/products/servlet/external-resources.html
>LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to