musthafa.aj 6 Posting Whiz

hai !

i want to make tcp communication using servlet, i tried this servlet program..

it will not work can anybody suggesting me...

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServer extends HttpServlet{
ServerSocket server;
Socket client;
DataInputStream dis;
int i;
public void service(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
PrintWriter out=response.getWriter();
ServerSocket server=new ServerSocket(9999);
response.setContentType("text/html");
Socket client=new Socket();
while(client.isConnected()){
System.out.println("Waiting for the Clients");

client=server.accept();
dis=new DataInputStream(client.getInputStream());
i=dis.readInt();
client.close();

out.println("<root>");
out.println("<Value>"+i+"</Value>");
out.println("</root>");
System.out.println("Read From the SErver-->"+i);
}



}
}

here is the client code..


import java.io.DataOutputStream;
import java.net.Socket;

public class Client {
public static void main(String args[]){
try{
System.out.println("Connecting...............");
Socket cli=new Socket("localhost",9999);
System.out.println("Connected");
DataOutputStream dos=new DataOutputStream(cli.getOutputStream());
dos.writeInt(12345);
dos.flush();
}catch(Exception e){
System.out.println("Exception is due to--->"+e);
}
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.