嗨有即時通訊非常新的Java編程,我真的堅持得到一個套接字服務器和客戶端工作,我想如何。Java套接字服務器的幫助
遇到的問題IM是...
服務器:
林看着來自客戶機的輸出,一旦客戶端打印「TIME」服務器將返回的消息如「時間就是.. 「。服務器執行此操作但不是直接發送,它似乎是在您第二次從客戶端發送消息時發送它。
這是因爲客戶端一直沒有連接嗎?
林相當肯定這種方法是錯誤的任何人都可以給我一些建議。
任何幫助將是偉大的。
盧克
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class MyServer {
public static void main(String[] args){
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try {
serverSocket = new ServerSocket(8888);
System.out.println("Listening :8888");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while(true){
try {
socket = serverSocket.accept();
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
dataOutputStream = new DataOutputStream(socket.getOutputStream());
System.out.println("ip: " + socket.getInetAddress());
System.out.println("message: " + dataInputStream.readUTF());
dataOutputStream.writeUTF("Hello!");
try{
String line = in.readLine();
if (line.contains("TIME")){
dataOutputStream.writeUTF("TIME IS....."); // ITS HERE THE PROBLEM MAY BE ?
{
} catch (IOException e){
System.out.println("Read failed");
System.exit(1);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(socket!= null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(dataInputStream!= null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(dataOutputStream!= null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
Android客戶端
UPDATE,我認爲這個問題是隻有當你發送郵件連接客戶端。我如何在這裏有一個閱讀循環,當我從客戶端發送數據時不會受到影響。
package com.exercise.AndroidClient;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidClient extends Activity {
EditText textOut;
TextView textIn;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textOut = (EditText)findViewById(R.id.textout);
Button buttonSend = (Button)findViewById(R.id.send);
textIn = (TextView)findViewById(R.id.textin);
buttonSend.setOnClickListener(buttonSendOnClickListener);
}
Button.OnClickListener buttonSendOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Socket socket = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
try {
socket = new Socket("192.168.1.101", 8888);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream.writeUTF(textOut.getText().toString());
textIn.setText(dataInputStream.readUTF());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if (socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null){
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataInputStream != null){
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}};
}
非常感謝您花時間回覆。這確實有助於這不是什麼即時通訊。似乎我的客戶端只在想要發送數據時才連接,因此在客戶端發送「時間」之後它不會接收數據。我想我需要客戶端循環等待輸入數據。這會不會因爲我感覺套接字無法同時傳輸和接收?再次歡呼 – Luke 2011-06-06 09:41:36
是的,你必須循環客戶端。您將一些東西發送到服務器,立即從流中讀取。這將等待傳入的數據。祝你好運;) – Baldur 2011-06-06 10:49:54
我現在把我的基本客戶端代碼,你可以快速看看,也許建議我如何適應這段代碼有一個閱讀循環在那裏,仍然能夠寫出數據。感謝你的寶貴時間。 – Luke 2011-06-06 11:28:21