对于安卓端的聊天程序,我通过数据输入流发送消息为
Socket sck = new Socket();
sck.connect(new InetAddress("192.168.1.91",1500),2000);
if(sck.isConnected())
{
DataOutputStream os = new DataOutputStream(sck.getOutputStream());
os.writeUTf(msg);
System.out.println("Message sent");
}
在服务器端,我的代码是
ServerSocket serv = new ServerSocket(1500);
Socket sock = serv.accept();
DataInputStream is = new DataInputStream(sock.getInputStream);
String ans = is.readUTF();
System.out.println("Got"+ans);
但是在服务器端,它似乎没有收到任何东西,仍在等待消息。但在客户端,它显示发送的消息。
这是我的完整代码。
package in.prasilabs.eagleeye;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class EagleClient extends Thread
{
public int ret = 0;
private String ip;
private int port;
private Socket sck;
private boolean estb = false;
private DataInputStream is;
private DataOutputStream os;
private String rmsg = null;
private String msg;
private String username;
private String password;
public String reply;
Data dt = new Data();
public EagleClient()
{
}
public EagleClient(String ip, int port, String username, String password)
{
this.ip = ip;
this.port = port;
this.username = username;
this.password = password;
}
public void run()
{
establish();
if(estb == true)
{
sendInfo(username,password);
}
}
public void establish()
{
int time_out = 2000;
try
{
System.out.println("trying to connect");
sck = new Socket();
sck.connect(new InetSocketAddress(dt.getIp(),dt.getServerPort()),time_out);
estb = true;
System.out.println("Established");
}
catch (UnknownHostException e)
{
ret =0;
e.printStackTrace();
}
catch (IOException e)
{
ret = 0;
// TODO Auto-generated catch block
e.printStackTrace();
}
if(estb == true)
{
try {
is = new DataInputStream(sck.getInputStream());
os = new DataOutputStream(sck.getOutputStream());
dt.setOS(os);
dt.setIS(is);
System.out.println("Messages are ready to send");
} catch (IOException e)
{
ret = 0;
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void sendInfo(String user,String password)
{
Data dt = new Data();
is = dt.getIS();
os = dt.getOS();
String drport = Integer.toString(dt.getdrPort());
try {
os.writeUTF("logn");
System.out.println("Sending user info");
os.writeUTF(dt.getUsername());
os.writeUTF(dt.getPassword());
os.writeUTF(drport);
reply = is.readUTF();
if(reply.equals("success"))
dt.setLoginStatus(true);
else
dt.setLoginStatus(false);
System.out.println("ACk recieved");
} catch (IOException e) {
ret = 0;
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int sendMessage(String msg)
{
Data dt = new Data();
os = dt.getOS();
is = dt.getIS();
this.msg = msg;
try {
os.writeUTF(msg);
System.out.println("Client :" +msg);
recieveMessage();
} catch (IOException e) {
ret = 0;
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
public void recieveMessage()
{
try {
System.out.println("Trying to get acknowledgement");
sck.setSoTimeout(2000);
rmsg = is.readUTF();
if(rmsg.equals(msg))
{
System.out.println("Ack recvd" +rmsg);
ret = 1;
}
else if(!msg.equalsIgnoreCase(rmsg))
{
//sendMessage();
Thread.sleep(100);
System.out.println("Retrying");
rmsg = is.readUTF();
System.out.println("Ack recvd" +rmsg);
ret = 1;
}
else
{
System.out.println("Message not recieved");
ret = 0;
}
} catch (IOException e) {
ret = 0;
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e){
e.printStackTrace();
}
}
}
尝试使用这样的东西:
String message = "";
while (!"end".equals(message)) {
if (is.available() != 0) {
message = dis.readUTF();
System.out.println(message);
}
}
我检查了这个 - 工作。在客户端发送消息之前,服务器必须启动。