This is my code for a Client to a Server that asks for IP address and Port
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EchoClientJDK121 extends JFrame {
private JTextField enter;
private JTextArea display;
ObjectOutputStream write;
ObjectInputStream read;
String message = "";
String IP;
String Port;
private JTextField Input;
private JTextField PORT;
public EchoClientJDK121( ) {
super( "Client" );
Container c = getContentPane( );
Input = new JTextField();
Input.setEnabled(false);
Input.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent i){
GetIP(i.getActionCommand());
Input.setText("ipAddress: ");
}
});
enter = new JTextField( );
enter.setEnabled( false );
enter.addActionListener(
new ActionListener( ) {
public void actionPerformed( ActionEvent e )
{
sendData( e.getActionCommand( ) );
enter.setText("");
}
}
);
GetPort(PORT);
c.add(PORT, BorderLayout.SOUTH);
c.add( enter, BorderLayout.NORTH );
c.add(Input, BorderLayout.SOUTH);
display = new JTextArea( );
c.add( new JScrollPane( display ),
BorderLayout.CENTER );
setSize( 300, 150 );
show( );
}
public void GetIP(String ip){
try{
IP = ip;
display.append( "\nIP>>>" + ip );
}
catch ( IOException cnfex ) {
display.append( "\nError writing object" );
}
}
public void GetPort(JTextField Port){
Port = new JTextField();
Input.setEnabled(false);
Input.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent p){
sendPort( e.getActionCommand( ) );
enter.setText("Port: ");
}
});
}
public void SendPort(String Port){
try{
String PORT = Port;
write.writeObject( "CLIENT>>> " + Port ); write.flush( );
display.append( "\nCLIENT>>>" + Port );
}
catch ( IOException cnfex) {
display.append( "\nError writing object" );
}
}
public void runEchoClientJDK121() {
Socket client;
try {
Integer.parseInt(IP);
Integer.parseInt(Port);
// Step 1: Create a Socket to make connection.
display.setText( "Attempting connection\n" );
client = new Socket( InetAddress.getByName( IP ), Port );
display.append( "Connected to: " +
client.getInetAddress( ).getHostName( ) );
// Step 2: Get the input and output streams.
write = new ObjectOutputStream( client.getOutputStream( ) );
write.flush( );
read = new ObjectInputStream( client.getInputStream( ) );
display.append( "\nGot I/O streams\n" );
// Step 3: Process connection.
enter.setEnabled( true );
do { //Print message as they are recieved.
try {
message = (String) read.readObject( ); //Blocking read.
display.append( "\n" + message );
display.setCaretPosition( display.getText( ).length( ) );
}
catch ( ClassNotFoundException cnfex ) {
display.append( "\nUnknown object type received" );
}
} while ( !message.equals( "SERVER>>> TERMINATE" ) );
// Step 4: Close connection.
display.append( "Closing connection.\n" );
write.close( ); read.close( ); client.close( );
}
catch ( EOFException eof ) {
System.out.println( "Server terminated connection" );
}
catch ( IOException e ) {
e.printStackTrace( );
}
}
private void sendData( String s ) {
try {
message = s;
write.writeObject( "CLIENT>>> " + s ); write.flush( );
display.append( "\nCLIENT>>>" + s );
}
catch ( IOException cnfex ) {
display.append( "\nError writing object" );
}
}
public static void main( String args[ ] ) {
EchoClientJDK121 app = new EchoClientJDK121( );
app.addWindowListener(
new WindowAdapter( ) {
public void windowClosing( WindowEvent e )
{ System.exit( 0 ); }
}
);
app.runEchoClientJDK121( );
}
}
I am getting the error:
Java 76: error: cannot find symbol
sendPort( e.getActionCommand( ) );
error: no suitable constructor found for Socket( InetAddress, String)
client = new Socket( InetAddress.getByName( IP), Port);