import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
class SocketClient extends JFrame
implements ActionListener {
JLabel text, clicked;
JButton button;
JPanel panel;
JTextField textField;
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
SocketClient(){ //Begin Constructor
text = new JLabel("Text to send over socket:");
textField = new JTextField(20);
button = new JButton("Click Me");
button.addActionListener(this);
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
getContentPane().add(panel);
panel.add("North", text);
panel.add("Center", textField);
panel.add("South", button);
} //End Constructor
public void actionPerformed(ActionEvent event){
Object source = event.getSource();
if(source == button){
//Send data over socket
String text = textField.getText();
out.println(text);
textField.setText(new String(""));
//Receive text from server
try{
String line = in.readLine();
System.out.println("Text received :" + line);
} catch (IOException e){
System.out.println("Read failed");
System.exit(1);
}
}
}
public void listenSocket(){
//Create socket connection
try{
socket = new Socket("124.243.213.225", 4444);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (UnknownHostException e) {
System.out.println("Unknown host: kq6py.eng");
System.exit(1);
} catch (IOException e) {
System.out.println("No I/O");
System.exit(1);
}
}
public static void main(String[] args){
SocketClient frame = new SocketClient();
frame.setTitle("Client Program");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
frame.listenSocket();
}
}
bloody_ninja -1 Junior Poster in Training
Recommended Answers
Jump to PostAnd your question is?
Jump to PostCorrect. Add an e.printStackTrace() to the catch block printing that message so you can see what type of IO Exception you're getting. Also, as long as the port is above 1024, anyone can use it (ports under that are only available to accounts with system admin privileges), but a very …
Jump to PostIs there a server running?
To use a client you have to have something to connect to, and that message means (usually) that there is nothing listening on that port, at that ip.
All 12 Replies
masijade 1,351 Industrious Poster Team Colleague Featured Poster
bloody_ninja -1 Junior Poster in Training
Alex Edwards 321 Posting Shark
Alex Edwards 321 Posting Shark
masijade 1,351 Industrious Poster Team Colleague Featured Poster
bloody_ninja -1 Junior Poster in Training
masijade 1,351 Industrious Poster Team Colleague Featured Poster
bloody_ninja -1 Junior Poster in Training
rkrijgsheld 0 Newbie Poster
masijade 1,351 Industrious Poster Team Colleague Featured Poster
bloody_ninja -1 Junior Poster in Training
masijade 1,351 Industrious Poster Team Colleague Featured Poster
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.