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 PostAn I/O Exception is being thrown.
You may find it useful to look up the Socket class in the java.net API
http://java.sun.com/javase/6/docs/api/java/net/Socket.html
…
Jump to PostBy the way, you may want to use a Port number greater than 5000, as suggested by my Instructor.
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.