I tried to run a code to create a GUI using java swing library to send a message to the localhost server.The GUI contains a text field to type the message and a button to send it to the server.Server code was contained in another class.
When I tried to run the code, a socket exception is shown in the console as:
java.net.SocketException: Permission denied: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
Please help me to get connection to server.
I am enclosing codes for both the GUI and the server here.
CLASS FOR CREATING GUI AND ESTABLISHING CONNECTION TO SERVER:
package org.myorg;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
import java.*io.IOException;
import java.io.PrintWriter;
import java.net.*;
import org.myorg.MessageServer;
public class SendMessage {
PrintWriter writer;
Socket sock;
JTextField text;
/*Creates GUI and adds SendButtonListener object to the button.
Calls setUpNetworking() method*/
public void go(){
JFrame frame=new JFrame();
JPanel panel=new JPanel();
text=new JTextField("Message",15);
JButton button=new JButton("Send");
button.setSize(5,5);
button.addActionListener(new SendButtonListener());
panel.add(text);
panel.add(button);
frame.getContentPane().add(BorderLayout.CENTER,panel);
frame.setSize(300,200);
frame.setVisible(true);
setUpNetworking();
}
/*Listens to action event of clicking the 'send' button.
The message from the text field is written to writer object for sending it
to server*/
class SendButtonListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
writer.println(text.getText());
writer.flush();
}catch(Exception ex){
ex.printStackTrace();
}
text.setText("message");
text.requestFocusInWindow();
}
}
/*Establishes socket connection to the server. Object of PrintWriter is
created*/
private void setUpNetworking(){
try {
sock=new Socket("127.0.0.1",50000);
writer=new PrintWriter(sock.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*Main method*/
public static void main(String[] args) {
SendMessage sendMessage=new SendMessage();
MessageServer server=new MessageServer();
sendMessage.go();
server.go();
}
}
SERVER CLASS
package org.myorg;
import java.io.*;
import java.net.*;
public class MessageServer {
Socket sock;
/*Accepts the socket connection and calls the read() method*/
public void go(){
try{
@SuppressWarnings("resource")
ServerSocket socket=new ServerSocket(50000);
sock=socket.accept();
read(sock);
}catch(IOException e){
e.printStackTrace();
}
}
/*Reads the message using InputStreamReader*/
public void read(Socket sock){
InputStreamReader stream;
try {
stream = new InputStreamReader(sock.getInputStream());
BufferedReader reader=new BufferedReader(stream);
System.out.println(reader.readLine());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}