โซน-เรดิโอ
โซน-อินไวท์
โซน-ซูล่าแบงค์กิ้ง
แขกผู้มาเยือน
เข้าสู่ระบบ
สมัครสมาชิก
ยินดีต้อนรับคุณ,
บุคคลทั่วไป
กรุณา
เข้าสู่ระบบ
หรือ
ลงทะเบียน
Please Login!
1 ชั่วโมง
1 วัน
1 สัปดาห์
1 เดือน
ตลอดกาล
หน้าแรก
เว็บบอร์ด
ช่วยเหลือ
ปฏิทิน
เข้าสู่ระบบ
สมัครสมาชิก (Invite)
ขอความกรุณาสละเวลา 5 นาที เพื่อกรอกแบบสอบถามของเว็บเรา
(ปิดรับทุกความเห็นภายในวันที่ 31 มีนาคมนี้)
Loading...
Zone-IT - โซนไอที สังคมดีๆบนโลกออนไลน์
»
Develop Zone!
»
Programming
(ผู้ดูแล:
ลั๊ลล๊าา I'm ตั้ม
)
ช่วยหน่อยติดปัญหาโปรแกรม ภาษาจาวา client/server
หน้า: [
1
]
« หน้าที่แล้ว
ต่อไป »
พิมพ์
ผู้เขียน
หัวข้อ: ช่วยหน่อยติดปัญหาโปรแกรม ภาษาจาวา client/server (อ่าน 136 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
Takashi_7
12
5
1
ช่วยหน่อยติดปัญหาโปรแกรม ภาษาจาวา client/server
«
เมื่อ:
2 กุมภาพันธ์ พ.ศ. 2553, 5.00 น. »
Direct Link
Tweet
Facebook
PM
ช่วยหน่อยติดปัญหาโปรแกรม ภาษาจาวา 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(); }
}
2 คูลเฟิร์มโดย:
bncom
,
ลั๊ลล๊าา I'm ตั้ม
บันทึกการเข้า
....สุภาพบุรุษวิศวะคอม
หน้า: [
1
]
พิมพ์
« หน้าที่แล้ว
ต่อไป »
กระโดดไป:
เลือกหัวข้อ:
-----------------------------
Secret Zone !
-----------------------------
===> Bit Colo
===> Web Hosting
-----------------------------
General Zone !
-----------------------------
=> สภากาแฟ
===> News Report
===> IT News
===> Gossip Zone
===> แนะนำตัว - ฝากเนื้อฝากตัว.
=> คลับโซนไอที
===> Asian Star !!
===> Love Club
===> คลับ"คนรักหนัง"
===> Mom Zone
===> Health Club !!
===> Zone-To Be Number One
===> ชมรมคนปักษ์ใต้
===> E-SAN !!!
===> B9 Club
=> กระทรวงศึกษาธิการ
=> Football Club
===> Football Clips
=> บอร์ดธรรมะ
=> ตลาดนัด
===> ค้าขายทั่วไป
===> คอมพิวเตอร์ และ ไอที
===> เครื่องใช้ไฟฟ้า&อิเล็กทรอนิคส์
===> สื่อบันเทิง
===> โทรศัพท์&การสื่อสาร
===> หนังสือ
===> รถและยานพาหนะ
===> อสังหาริมทรัพย์
===> เสื้อผ้าและเครื่องประดับ
===> ของเล่น ของสะสม กีฬา
===> ตลาดแรงงาน จ้างงาน หางาน
===> อาหาร และสุขภาพ
===> ห้องค้าขาย สินค้า MIDI Karaoke ทุกชนิด
=> แนะนำหมู่บ้าน
-----------------------------
Zone-IT Press Center
-----------------------------
=> ประกาศต่างๆ ของโซนไอที
===> พิพิธภัณฑ์ โซนไอที
=====> โครงการจัดทำเสื้อโซนไอทีรุ่นที่ 1
=> กองทุนหมู่บ้าน
===> ประวัติรายรับรายจ่ายประจำเดือน
=> คู่มือหมู่บ้าน
=> ติดต่อสอบถาม-คำแนะนำ-ติชม
-----------------------------
IT Zone !
-----------------------------
=> Q&A IT Problem
=> Tip - Articles
=> ร้านหนังสือ
===> E-Book - Computer Internet
===> E-Book - general
=> Security Zone
=> Graphics Zone
===> Graphic Tips & Tutorials
===> 3D & Video Graphic
===> Graphics Download
=> Open Source !!
===> Ubuntu Fanzone
===> Web Server (Open Source)
=> Apple / Mac.
===> MacOSX86
=> Windows 7 Zone!
===> Download Program For Vista & Window 7 !!
=> Hi5 Zone!!
=> Network Zone
=> หมาย่างแฟนคลับ
===> Firefox Q&A and FAQs
=====> Firefox FAQs
===> Firefox Article
===> Firefox News and Talk
===> Firefox Neighborhood
=> Mobile Zone
===> Mobile Q&A
===> Mobile Apps. & Games
===> Mobile Accessories Download
===> All Mobile Request
===> PocketPC PDA ,Plam
=> BitTorrent Zone !!
===> พูดคุย เกี่ยวกับ Bittorrent
===> แนะนำ Bittorrent เปิดใหม่
===> Share Invite Zone!
===> Bit Colo
-----------------------------
Develop Zone!
-----------------------------
=> Webbuilder Zone
===> Generals Webbuilder
===> WordPress [WP]
=====> WordPress Themes
=====> WordPress Plugins
===> Simple Machines Forum [ SMF ]
=====> ห้องสมุด SMF
===> Joomla
=====> Joomla Article
=====> Joomla Template Download
===> WEB Hosting & Domain Name
=> Programming
-----------------------------
Downloads Zone !
-----------------------------
=> Download Non Program !!
-----------------------------
Game Zone !
-----------------------------
=> Game Online Zone !!
-----------------------------
Fun Zone !
-----------------------------
=> World Of Story And Cartoon !!
===> Cartoons World !!
===> Funny Joke
===> ห้องขนหัวลุกๆ
===> Writer Board
=> PicPost
-----------------------------
Relax Zone !
-----------------------------
=> VDO CLIPS
=> TV & Radio Online
===> Free TV and Cable TV Online
===> Internet Broadcasting and Radio Online
-----------------------------
Music Zone !
-----------------------------
=> ชุมชนมิดี้โซนไอที (Zone-IT's MIDI Community)
===> ห้องสมุด MIDI & Karaoke
=====> ความรู้เบื้องต้นสำหรับมือใหม่
=====> โปรแกรมเล่นคาราโอเกะ Li-ke Sing
=====> ทิปและเทคนิคในการใช้งานโปรแกรม MIDI&Karaoke
=====> ระบบเครื่องเสียงและอื่นๆ
===> ห้องโชว์ผลงาน
=> SoundFont
===> SF Talk
===> SF Knowledge
===> ห้องแจก SF
===> ห้องทดสอบและวิจารณ์เสียง SF
===> ห้องประกาศและสำรวจความเห็น (จากทีมงาน)
7
กำลังโหลด...