开发者

how do I make this compile?

This is my code snippet:

import java.awt.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;

class chatboxServer {
  JFrame fr;
  JPanel p;
  JTextArea ta;
  JButton send;

  chatboxServer() {
    fr=new JFrame("ChatBox_SERVER");
    p=new JPanel();
    ta=new JTextArea();
    ta.setRows(20);
    ta.setColumns(20);
    send=new JButton("send");
    fr.add(p);
    p.add(ta);
    p.add(send);
    fr.setVisible(true);
    fr.setSize(500,500);
    fr.setResizable(false);
    try {                                    // Making server listen to port number 3000
      ServerSocket s=new ServerSocket(3000);
    } catch(Exception exc) {
      JOptionPane.showMessageDialog(new JFrame(),"Cannot Listen :3000");
    }
    try {
      Socket s=null;
      s=ServerSocket.accept();
    } catch(Exception exc)
      JOptionPane.showMessageDialog(new JFrame(),"Accept Failed :3000");
    } 
  }
  public static void main(String args[]) {
    new chatboxServer();
  }
}

The error that I get is:

Non static method cannot referenced from static context.

Please explain how can I can m开发者_JAVA百科ake this compile?


I think the problem is that in the line s=ServerSocket.accept() you're trying to call accept on the ServerSocket class rather than on an instance of the class. accept is an instance method not a static method.

You probably want something like:

ServerSocket s = new ServerSocket(3000);
Socket sock = s.accept();

Possibly just wrapping these in a single try/catch. e.g

ServerSocket serverSocket;
Socket socket;
try {              
    serverSocket = new ServerSocket(3000);
    socket = serverSocket.accept();
} catch (Exception e) {
    JOptionPane.showMessageDialog(new JFrame(), "Cannot Listen and accept on port 3000");
}


try this:

import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class chatboxServer
{
    protected JTextArea ta = new JTextArea();;
    protected JButton send;
    protected JFrame fr = new JFrame("ChatBox_SERVER");
    protected JPanel p = new JPanel();

    chatboxServer()
    {
        ta.setRows(20);
        ta.setColumns(20);
        send = new JButton("send");
        fr.add(p);
        p.add(ta);
        p.add(send);
        fr.setVisible(true);
        fr.setSize(500, 500);
        fr.setResizable(false);
        ServerSocket s = null;

        try
        { // Making server listen to port number 3000
            s = new ServerSocket(3000);
        }
        catch (Exception excep)
        {
            JOptionPane.showMessageDialog(new JFrame(), "Cannot Listen :3000");
        }

        Socket sock = null;

        try
        {
            sock = s.accept();
        }
        catch (Exception ex)
        {
            Logger.getLogger(chatboxServer.class.getName()).log(Level.SEVERE, null, ex);
        }

        JOptionPane.showMessageDialog(new JFrame(), "Accept Failed :3000");
    }

    public static void main(String args[])
    {
        new chatboxServer();
    }
}

It's still got issues, like using port 3000, but at least it compiles.


You will need to specify that the chatboxServer() constructor is static.

for instance

private static chatboxServer() {

This should fix your issue.


You should write something like this:

ServerSocket server;
Socket socket;
 try {              
  server =new ServerSocket(3000);
  socket = server.accept();
 } catch(Exception exc) {
    JOptionPane.showMessageDialog(new JFrame(),"Cannot Listen & accept on port :3000");
  }


You are assigning a ServerSocket to s in one block, and use another ServerSocket also named s in the following block, this is the first problem. In the second block you call accept on the ServerSocket class instead of calling it on an instance...

Following code should be changed (just to make it work):

try { // Making server listen to port number 3000
      ServerSocket s=new ServerSocket(3000);
    } catch(Exception exc) {
      JOptionPane.showMessageDialog(new JFrame(),"Cannot Listen :3000");
    }
    try {
      Socket s=null;
      s=ServerSocket.accept();
    } catch(Exception exc)
      JOptionPane.showMessageDialog(new JFrame(),"Accept Failed :3000");
    } 

to:

 ServerSocket s;
try { // Making server listen to port number 3000
      s=new ServerSocket(3000);
    } catch(Exception exc) {
      JOptionPane.showMessageDialog(new JFrame(),"Cannot Listen :3000");
    }
    try {
      s.accept();
    } catch(Exception exc)
      JOptionPane.showMessageDialog(new JFrame(),"Accept Failed :3000");
    } 


I think that the problematic line is the one below ;

s=ServerSocket.accept();

accept method of ServerSocket is not static method, it is instance method.. It should be corrected as below lines :

 ServerSocket s = null;
 Socket socket=null;
 try {                                    // Making server listen to port number 3000
      s=new ServerSocket(3000);
    } catch(IOException exc) {
      JOptionPane.showMessageDialog(fr,"Cannot Listen :3000");
      return; 
    }
    try {

      socket=s.accept();
    } catch(IOException exc)
      JOptionPane.showMessageDialog(fr,"Accept Failed :3000");
    } 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜