2011-06-05 102 views
1

嗨有即时通讯非常新的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(); 
} 
} 
} 
}}; 
} 

回答

3

之后dataOutputStream.writeUTF()行写dataOutputStream.flush();。这将通过发送所有数据。

0

看来您正在从流中读取以输出消息。

System.out.println("message: " + dataInputStream.readUTF()); 

于是命令已经从物流中除去,当您尝试运行

String line = in.readLine(); 

你应该尝试从流读取命令到一个变量输出消息之前,然后检查是否是字符串包含“TIME”。

另外为什么使用两种不同的方法从流中读取?你可以使用BufferedReader并放弃DataInputStream。你可以使用PrintWriter而不是DataOutputStream。

事情是这样的:

out = new PrintWriter(socket.getOutputStream(), true); 

然后:

out.println("Time is..."); 

希望这有助于。

+0

非常感谢您花时间回复。这确实有助于这不是什么即时通讯。似乎我的客户端只在想要发送数据时才连接,因此在客户端发送“时间”之后它不会接收数据。我想我需要客户端循环等待输入数据。这会不会因为我感觉套接字无法同时传输和接收?再次欢呼 – Luke 2011-06-06 09:41:36

+0

是的,你必须循环客户端。您将一些东西发送到服务器,立即从流中读取。这将等待传入的数据。祝你好运;) – Baldur 2011-06-06 10:49:54

+0

我现在把我的基本客户端代码,你可以快速看看,也许建议我如何适应这段代码有一个阅读循环在那里,仍然能够写出数据。感谢你的宝贵时间。 – Luke 2011-06-06 11:28:21