• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What's wrong here!

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I wrote on applet which is as follows in which I am trying to use socket to connect to server. In TextField1 whatever i add , i want to write this string to server and want to get response back in string 2.
import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
public class MyApplet extends Applet implements ActionListener{
Socket localSocket;
PrintWriter out;
BufferedReader in;
BufferedReader kbdInput;
String s;
String t1,t2;
private Button b;
public void init(){
setLayout(new FlowLayout());
TextField t1 = new TextField(20);
t1.setText("Please Enter the value here");
add(t1);
TextField t2 = new TextField(20);
add(t2);
b = new Button("Connect");
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b)
//Create a socket
try {
localSocket = new Socket("10.3.158.222",8192);
//Setup data stream in and out of socket and from KeyBoard
in = new BufferedReader(new InputStreamReader(localSocket.getInputStream()));
out= new PrintWriter(localSocket.getOutputStream());
//While we have a connection
while(true)
{
//Read Texfield value
out.println(t1);
//flush the buffer if not full!
out.flush();
// read incoming string from socket
//System.out.println(in.readLine());
t2 = in.readLine();
}
}
catch(UnknownHostException unc)
{
System.out.println("Connection why not connected");
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
}
}
Please let me know what is wrong here in this code. I can compiled but when I run it, when i entered the value and click the button, i am not getting response in text2.
Thanks in advance,
ANgela
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have only given the client side code. Sockets normally need to connect to a ServerSocket. Can you show us what your server side code looks like?
Regards
Graeme
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela,
1)You use setText() & getText() functions of textbox component to
pass the text & display text respectively.You have used only t1
& t2 for it.That may be the problem.
2)Moreover you haven't given your server program.
Try the first one out.I think this will solve your purpose.
Regards
Lala
reply
    Bookmark Topic Watch Topic
  • New Topic