ช่วยหน่อยติดปัญหาโปรแกรม ภาษาจาวา client/server
ช่วยหน่อยติดปัญหาโปรแกรม ภาษาจาวา client/server
คือปัญหา ในการรันฝั่งไคลเอ้น ในรูป GUI ไม่ส่งข้อมูลไปยัง server
รบกวนผู้มีความรู้ ช่วยหน่อย นะคับ
...ขอบคุณมากๆ...นะคับ สำหรับ...คำแนะ
//GuiTCPClientConvert.java
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuiTCPClientConvert extends JFrame implements ActionListener
{
public static String message;
public static String response;
private static InetAddress host;
private static int PORT = 1234;
private static JTextArea display;
private static JLabel f1;
private static JLabel f2;
private static JTextField inField;
private static JTextField outField;
private static JButton con;
//private DatagramSocket socket;
public GuiTCPClientConvert()
{
super("[Client] Conver Kilomate to Mile");
Container c = getContentPane();
c.setLayout( new FlowLayout());
con = new JButton("Convert");
f1 = new JLabel("Kilomate");
inField = new JTextField(20);
f2 = new JLabel("Mile");
outField = new JTextField(20);
display = new JTextArea("test");
c.add(f1);
c.add(inField);
c.add(con);
c.add(f2);
c.add(outField);
//c.add(new JScrollPane( display),BorderLayout.SOUTH);
con.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700,100);
//pack();
setLocationRelativeTo(null);
setVisible(true);
try
{
host = InetAddress.getLocalHost();
}
catch(UnknownHostException uhEx)
{
System.out.println("Host ID not found!");
System.exit(1);
}
accessServer();
} // end of GuiTCPClientConvert
private static void accessServer()
{
Socket link = null; //Step 1.
try
{
link = new Socket(host,PORT); //Step 1.
DataInputStream is = new DataInputStream( link.getInputStream() );
PrintWriter output =new PrintWriter(link.getOutputStream(),true); //Step 2.
DataInputStream input = new DataInputStream( System.in );
//String message, response;
do
{
System.out.print("\n### PROGRAM CONVERT KILOMATE TO MILE ###\n");
System.out.print("Enter Number Kilomate : ");
message = input.readLine();
output.println(message); //Step 3.
response = is.readLine(); //Step 3.
System.out.println("\nSERVER> "+response);
GuiTCPClientConvert.inField.setText(message);
GuiTCPClientConvert.outField.setText(response);
}while (!message.equals("***CLOSE***"));
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
try
{
System.out.println("\n [client] Closing connection... ");
link.close(); //Step 4.
}
catch(IOException ioEx)
{
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
public void actionPerformed(ActionEvent e)
{
//System.out.println("Use actionperform!!!");
message = inField.getText();
//System.out.println(message);
//System.out.println(response);
outField.setText(response);
}
public static void main( String args[] )
{
new GuiTCPClientConvert(); }
}
---------------------------------------------------
//GuiTCPServerConvert.java
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuiTCPServerConvert extends JFrame
{
private static ServerSocket servSock;
private static int PORT = 1234;
private static JTextField outField;
private static JTextArea display;
public GuiTCPServerConvert()
{
super("[Server] Convert Kilomate to Mile");
Container c = getContentPane();
display = new JTextArea();
outField = new JTextField(20);
c.add(new JScrollPane( display),BorderLayout.CENTER );
//c.add(new JScrollPane( outField ),BorderLayout.SOUTH );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
show();
//System.out.println("Opening port.....\n");
display.append("Opening port.....\n");
try
{
servSock = new ServerSocket(PORT); //Step 1.
}
catch(IOException ioEx)
{
//System.out.println("Unable to attach to port!");
display.append("Unable to attach to port!");
//System.exit(1);
}
do
{
handleClient();
}
while (true);
} //end GuiTCPServerConvert
private static void handleClient()
{
Socket link = null; //Step 2.
try
{
link = servSock.accept(); //Step 2.
DataInputStream is = new DataInputStream( link.getInputStream() ); //Step3.
PrintWriter output = new PrintWriter(link.getOutputStream(),true); //Step3.
int numMessages = 0;
String message = is.readLine(); //Step 4.
//String message = input.nextLine().toUpperCase(); //แปลงตัวเล็กเปงตัวใหญ่
while (!message.equals("***CLOSE***"))
{
//System.out.println("Message received.");
GuiTCPServerConvert.display.setText("Message received");
numMessages++;
String msg1 = message;
double k2m = 0.621;
double msg2 = Integer.parseInt(msg1);
double msg = msg2*k2m;
output.println("Message[" + numMessages +"] = " + msg + " Mile");
message = is.readLine();
} //end of while
output.println(numMessages + " messages received.");//Step 4.
} //end of try
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
try
{
//System.out.println("\n[server]Closing connection... ");
GuiTCPServerConvert.display.setText("\n [server]Closing connection... ");
link.close(); //Step 5.
}
catch(IOException ioEx)
{
//System.out.println("Unable to disconnect!");
GuiTCPServerConvert.display.setText("Unable to disconnect!");
System.exit(1);
}
} //end finally
} //end of handleClient()
public static void main( String args[] )
{ new GuiTCPServerConvert(); }
}
คือปัญหา ในการรันฝั่งไคลเอ้น ในรูป GUI ไม่ส่งข้อมูลไปยัง server
รบกวนผู้มีความรู้ ช่วยหน่อย นะคับ
...ขอบคุณมากๆ...นะคับ สำหรับ...คำแนะ
//GuiTCPClientConvert.java
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuiTCPClientConvert extends JFrame implements ActionListener
{
public static String message;
public static String response;
private static InetAddress host;
private static int PORT = 1234;
private static JTextArea display;
private static JLabel f1;
private static JLabel f2;
private static JTextField inField;
private static JTextField outField;
private static JButton con;
//private DatagramSocket socket;
public GuiTCPClientConvert()
{
super("[Client] Conver Kilomate to Mile");
Container c = getContentPane();
c.setLayout( new FlowLayout());
con = new JButton("Convert");
f1 = new JLabel("Kilomate");
inField = new JTextField(20);
f2 = new JLabel("Mile");
outField = new JTextField(20);
display = new JTextArea("test");
c.add(f1);
c.add(inField);
c.add(con);
c.add(f2);
c.add(outField);
//c.add(new JScrollPane( display),BorderLayout.SOUTH);
con.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(700,100);
//pack();
setLocationRelativeTo(null);
setVisible(true);
try
{
host = InetAddress.getLocalHost();
}
catch(UnknownHostException uhEx)
{
System.out.println("Host ID not found!");
System.exit(1);
}
accessServer();
} // end of GuiTCPClientConvert
private static void accessServer()
{
Socket link = null; //Step 1.
try
{
link = new Socket(host,PORT); //Step 1.
DataInputStream is = new DataInputStream( link.getInputStream() );
PrintWriter output =new PrintWriter(link.getOutputStream(),true); //Step 2.
DataInputStream input = new DataInputStream( System.in );
//String message, response;
do
{
System.out.print("\n### PROGRAM CONVERT KILOMATE TO MILE ###\n");
System.out.print("Enter Number Kilomate : ");
message = input.readLine();
output.println(message); //Step 3.
response = is.readLine(); //Step 3.
System.out.println("\nSERVER> "+response);
GuiTCPClientConvert.inField.setText(message);
GuiTCPClientConvert.outField.setText(response);
}while (!message.equals("***CLOSE***"));
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
try
{
System.out.println("\n [client] Closing connection... ");
link.close(); //Step 4.
}
catch(IOException ioEx)
{
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
}
public void actionPerformed(ActionEvent e)
{
//System.out.println("Use actionperform!!!");
message = inField.getText();
//System.out.println(message);
//System.out.println(response);
outField.setText(response);
}
public static void main( String args[] )
{
new GuiTCPClientConvert(); }
}
---------------------------------------------------
//GuiTCPServerConvert.java
import java.io.*;
import java.net.*;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuiTCPServerConvert extends JFrame
{
private static ServerSocket servSock;
private static int PORT = 1234;
private static JTextField outField;
private static JTextArea display;
public GuiTCPServerConvert()
{
super("[Server] Convert Kilomate to Mile");
Container c = getContentPane();
display = new JTextArea();
outField = new JTextField(20);
c.add(new JScrollPane( display),BorderLayout.CENTER );
//c.add(new JScrollPane( outField ),BorderLayout.SOUTH );
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
show();
//System.out.println("Opening port.....\n");
display.append("Opening port.....\n");
try
{
servSock = new ServerSocket(PORT); //Step 1.
}
catch(IOException ioEx)
{
//System.out.println("Unable to attach to port!");
display.append("Unable to attach to port!");
//System.exit(1);
}
do
{
handleClient();
}
while (true);
} //end GuiTCPServerConvert
private static void handleClient()
{
Socket link = null; //Step 2.
try
{
link = servSock.accept(); //Step 2.
DataInputStream is = new DataInputStream( link.getInputStream() ); //Step3.
PrintWriter output = new PrintWriter(link.getOutputStream(),true); //Step3.
int numMessages = 0;
String message = is.readLine(); //Step 4.
//String message = input.nextLine().toUpperCase(); //แปลงตัวเล็กเปงตัวใหญ่
while (!message.equals("***CLOSE***"))
{
//System.out.println("Message received.");
GuiTCPServerConvert.display.setText("Message received");
numMessages++;
String msg1 = message;
double k2m = 0.621;
double msg2 = Integer.parseInt(msg1);
double msg = msg2*k2m;
output.println("Message[" + numMessages +"] = " + msg + " Mile");
message = is.readLine();
} //end of while
output.println(numMessages + " messages received.");//Step 4.
} //end of try
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
finally
{
try
{
//System.out.println("\n[server]Closing connection... ");
GuiTCPServerConvert.display.setText("\n [server]Closing connection... ");
link.close(); //Step 5.
}
catch(IOException ioEx)
{
//System.out.println("Unable to disconnect!");
GuiTCPServerConvert.display.setText("Unable to disconnect!");
System.exit(1);
}
} //end finally
} //end of handleClient()
public static void main( String args[] )
{ new GuiTCPServerConvert(); }
}
ถ้า javascript ละพอช่วยได้ละครับนิเล่น java เลยคงต้องรอผู้รู้มาตอบ