ผมต้องทำโปรแจ๊ค ส่งค่าไปมา ระหว่าง server - client

เลยซื้อหนังสือมาลองเขียน ผลปรากฏว่า ส่งไปไม่ได้อะคับ

ฝั่ง server

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

public class SimpleServer implements Runnable{

public void run(){

try{

    ServerSocket serverSocket = new ServerSocket(4449);
    Socket client = null;
    System.out.println("Server Strated");
    String message = "Goodbye";

while(true){
client = serverSocket.accept();

    System.out.println("Client accept:" + client);	

try{

BufferedReader in = new BufferedReader(

                 new InputStreamReader(client.getInputStream()));

String str = in.readLine();

System.out.println("read:" + str);

PrintWriter out = new PrintWriter(new BufferedWriter(

              new OutputStreamWriter(client.getOutputStream())),true);

out.println(message);

out.close();

in.close();

}catch(Exception e){

	client.close();

	System.out.println("close");

	System.out.println(e.getMessage());

	e.printStackTrace();}

}
}catch(Exception e){

	System.out.println(e.getMessage());}

}	
public static void main(String a[]){

	Thread desktopServerThread=new Thread(new SimpleServer());

	desktopServerThread.start();

}

}

ฝั่ง client

package com.kku.cs;

import android.app.Activity;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.io.PrintWriter;

import java.net.Socket;

import android.view.View;

import android.view.View.OnClickListener;

import android.os.Bundle;

import android.util.Log;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;

public class AndriodClient extends Activity {

public final String DEBUG_TAG = "AndroidClient";

private TextView mTextView = null;

private EditText mEditText = null;

private Button mButton = null;

private Socket socket = null;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


mButton =(Button)findViewById(R.id.Button01);

mTextView =(TextView)findViewById(R.id.TextView01);

mEditText =(EditText)findViewById(R.id.EditText01);

mButton.setOnClickListener(new OnClickListener(){

public void onClick(View v){

	String message = mEditText.getText().toString()+ "\r\n";

try {
	socket = new Socket("192.168.1.19",4449);

	PrintWriter out = new PrintWriter(new BufferedWriter(
		new OutputStreamWriter(socket.getOutputStream())),true);
	out.println(message);


	BufferedReader br = new BufferedReader(new InputStreamReader(
			socket.getInputStream()));
	String msg = br.readLine();


	if(msg != null){
		mTextView.setText(mTextView.getText()+"\n"+msg);
		}
	else {
		mTextView.setText("Data error!");
	}
		out.close();
		br.close();
		socket.close();
	} catch(Exception e){
	Log.e(DEBUG_TAG, e.toString());
	}
	}
});
}

}

Hiring! บริษัทที่น่าสนใจ

Carmen Software company cover
Carmen Software
Hotel Financial Solutions
Next Innovation (Thailand) Co., Ltd. company cover
Next Innovation (Thailand) Co., Ltd.
We are web design with consulting & engineering services driven the future stronger and flexibility.
KKP Dime company cover
KKP Dime
KKP Dime บริษัทในเครือเกียรตินาคินภัทร
Kiatnakin Phatra Financial Group company cover
Kiatnakin Phatra Financial Group
Financial Service
Fastwork Technologies company cover
Fastwork Technologies
Fastwork.co เว็บไซต์ที่รวบรวม ฟรีแลนซ์ มืออาชีพจากหลากหลายสายงานไว้ในที่เดียวกัน
Thoughtworks Thailand company cover
Thoughtworks Thailand
Thoughtworks เป็นบริษัทที่ปรึกษาด้านเทคโนโยลีระดับโลกที่คว้า Great Place to Work 3 ปีซ้อน
Iron Software company cover
Iron Software
Iron Software is an American company providing a suite of .NET libraries by engineer for engineers.
CLEVERSE company cover
CLEVERSE
Cleverse is a Venture Builder. Our team builds several tech companies.
Nipa Cloud company cover
Nipa Cloud
#1 OpenStack cloud provider in Thailand with our own data center and software platform.
Bangmod Enterprise company cover
Bangmod Enterprise
The leader in Cloud Server and Hosting in Thailand.
CIMB THAI Bank company cover
CIMB THAI Bank
MOVING FORWARD WITH YOU - CIMB is the leading ASEAN Bank
Bangkok Bank company cover
Bangkok Bank
Bangkok Bank is one of Southeast Asia's largest regional banks, a market leader in business banking
MuvMi (Urban Mobility Tech Co.,Ltd.) company cover
MuvMi (Urban Mobility Tech Co.,Ltd.)
Shape the future of urban mobility towards affordable, clean, and safe solutions
T.N. Digital Solution Co., Ltd. company cover
T.N. Digital Solution Co., Ltd.
TNDS has been involving in every first move of banking’s major digital transformation.
KBTG - KASIKORN Business-Technology Group company cover
KBTG - KASIKORN Business-Technology Group
KBTG - "The Technology Company for Digital Business Innovation"
Siam Commercial Bank Public Company Limited company cover
Siam Commercial Bank Public Company Limited
"Let's start a brighter career future together"
Icon Framework co.,Ltd. company cover
Icon Framework co.,Ltd.
Global Standard Platform for Real Estate แพลตฟอร์มสำหรับธุรกิจอสังหาริมทรัพย์ครบวงจร มาตรฐานระดับโลก
REFINITIV company cover
REFINITIV
The Financial and Risk business of Thomson Reuters is now Refinitiv
H LAB company cover
H LAB
Re-engineering healthcare systems through intelligent platforms and system design.
The Gang Technology Co., Ltd. company cover
The Gang Technology Co., Ltd.
We're a Digital Agency that helps our customers transform their business into digital with ease.
LTMH company cover
LTMH
LTMH มุ่งเน้นการพัฒนาผลิตภัณฑ์ที่สามารถช่วยพันธมิตรของเราให้บรรลุเป้าหมาย
Seven Peaks company cover
Seven Peaks
We Drive Digital Transformation
Wisesight (Thailand) Co., Ltd. company cover
Wisesight (Thailand) Co., Ltd.
The Best Choice For Handling Social Media · High Expertise in Social Data · Most Advanced and Secure
MOLOG Tech company cover
MOLOG Tech
We are Modern Logistic Platform, Specialize in WMS, OMS and TMS.
Data Wow Co.,Ltd company cover
Data Wow Co.,Ltd
We enable our clients to realize increased productivity by solving their most complex issues by Data
LINE Company Thailand company cover
LINE Company Thailand
LINE, the world's hottest mobile messaging platform, offers free text and voice messaging + Call
LINE MAN Wongnai company cover
LINE MAN Wongnai
Join our journey to becoming No.1 food platform in Thailand

ข้อมูลน้อยเกินครับ รบกวนช่วยบอกหน่อยว่าติดปัญหาตรงไหน อะไรยังไง ขอแบบละเอียดหน่อยก็จะดีครับ

อยากให้ลองนึกถึงว่า คุณไม่ได้เป็นคนเขียนโปรแกรมเองแต่ต้องแก้โปรแกรมโดยที่ไม่รู้อะไรเลยดูครับ

ผมลองรัน ฝั่ง server ก็รอรับปกติ แต่ทางด้าน client ส่งข้อมูลไป ก็จะ ปริ้น data error! ตลอดอะคับ

Namo Thu, 25/08/2011 - 01:37

ในLogcat มันขึ้นแบบนี้อะคับ

default buffer size used in BufferedWriter/BuferedReader constructor . It would be better to be explicit if an 8k-char bufeer id required

PaPaSEK Thu, 25/08/2011 - 13:56

ยาวมาเลยแฮะ ผมพูดตามตรงว่าผมไม่คุ้นเคยกับ Class ต่างๆ ที่คุณเรียกใช้ครับ

สิ่งที่ผมแนะนำได้คือ

  1. คุณต้องรู้จัก Debug เป็น คุณจะได้ตรวจสอบได้ว่า Object แต่ละตัวทำงานได้ครบตามที่คุณต้องการรึเปล่า

  2. คุณต้องรู้จัก Constructor ของแต่ละ Class เห็นได้ว่าคุณยังไม่แม่นเรื่อง Constructor จากตรงนี้

default buffer size used in BufferedWriter/BuferedReader constructor . It would be better to be explicit if an 8k-char bufeer id required

ในตอนทดสอบก็ถือว่ายอมรับได้กับการเขียน Constructor แบบนี้ แต่ในกรณีที่ข้อมูลมันใหญ่มากๆ คุณก็ควรหั่นข้อมูลออกเป็นส่วนๆ ตามที่ Java แนะนำครับ

  1. ผมพบว่าการเขียน Code แบบ one - line แบบที่คุณทำมันยากต่อการ debug มากๆ คุณไม่ควรเขียน Code แบบ functional programming แบบนี้

จริงอยู่ว่ามันทำให้ code สะอาดตา แต่คุณจะ Debug อะไรแทบไม่ได้เลย เพราะมันไม่มีตัวแปรที่เก็บค่าใดๆ ให้คุณเห็น เมื่อคุณ debug

ขออภัยครับ แนะนำได้เท่านี้

ปล.เผื่อจะมีท่านต่อไปมาแนะนำ อยากให้คุณหาสาเหตุให้เจอว่า

  • client ส่งข้อมูลได้ - แต่โดน proxy ดักไว้
  • client ส่งข้อมูลได้ - แต่ server รับข้อมูลไม่ได้
  • client ส่งข้อมูลไม่ได้

3 ข้อนี้มีผลต่อการเขียนโปรแกรมต่อไปของคุณครับ

ลองใช้ telnet เป็น client แทนไปก่อนครับ ดูว่า server ทำงานถูกต้องหรือไม่ ถ้าทำงานได้ถูกค่อยไปไล่ดูโค้ดส่วน client

อ้อ... ใน client อย่าลืมไป allow android.permission.INTERNET ใน Manifest ด้วยนะครับ