By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

I am in a horrowble situation. Plz help me!!!!(Socket connection) near to deadline.

P: 28
Hi !
I am working on linux platform.
I have some problem with sockets.I want to communicate with a C machine through sockets.

I find few problems in it.
The way i am creating socket in my jsp file is:

<%!

ServerSocket MyServer;
Socket Mysocket;
File file = new File("/home/sait9/rama/TomcatJSPExample/web/file1.txt");
//getting data from file to send to C -program
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
DataOutputStream dos=null;

%>

<%
int port = Integer.parseInt(request.getParameter("port1"));//port to sent to C-pro
try{
MyServer =new ServerSocket(port);
Mysocket = MyServer.accept();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
dos=new DataOutputStream(Mysocket.getOutputStream());
while (dis.available() != 0)
{
dos.writeBytes(dis.readLine());
}


fis.close();
bis.close();
dis.close();
dos.flush();
dos.close();
Mysocket.close();
MyServer.close();
}
catch(Exception e)
{

System.out.println("Encountered an error!!!!"+e);
}



Here i have two problems,

1.)I am unable to use the same port if i want to run it once again(im getting it as done in the browser).

2.)All the values from the file are not being sent C-program.I mean sometimes i can get all the values and some time half &some times nothing or one byte.why so...............

I must get the values perfectly dumped to C program all the times.How could I.

PLz! reply me soon.I am near to the dead line..
Nov 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: 25
i used your code in a java program to read a tex file
and the i checked the output at http://localhost:4242/ it showed it correctly
Nov 16 '06 #2

P: 28
It is working but i could not recieve values at c-program every time i reload it.And even i need to change the ports when i reload it.
Nov 16 '06 #3

P: 25
to my understanding
the c program is running on a different machine
this c program gives the port at which it will listen this is passed to the jsp
the jsp then sends the data to the c program
is this correct.
Nov 16 '06 #4

P: 25
try this url it might help


http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html
Nov 16 '06 #5

Post your reply

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