我的这个java环境下PC机客户端和服务端可以通讯 但客户端移到android上后会出错 我debug调试了下错误行为: String userinfo=ed2.getText()+":"+InetAddress.getLocalHost().toString(); 服务端报错: Error:java.io.EOFException求大神们解决
android虚拟机客户端代码package a.b;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.StringTokenizer;import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;public class ChatClientActivity extends Activity implements OnClickListener, Runnable {
private EditText ed1;
private EditText ed2;
private EditText ed3;
private EditText ed4;
private EditText ed5;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View button_connect = findViewById(R.id.button_connect);
button_connect.setOnClickListener(this);
View button_exit = findViewById(R.id.button_exit);
button_exit.setOnClickListener(this);
View button_send = findViewById(R.id.button_send);
button_send.setOnClickListener(this);
EditText ed1= (EditText) findViewById(R.id.editText_serveraddress);
EditText ed2 = (EditText) findViewById(R.id.editText_carname);
EditText ed3 = (EditText) findViewById(R.id.editText_content);
EditText ed4 = (EditText) findViewById(R.id.editText_showcontent);
EditText ed5 = (EditText) findViewById(R.id.editText_showname);
// ed1.setText("localhost");
}
Socket soc=null; //定义连接套接字
DataInputStream dis=null; //定义用来实现客户端接受服务器数据的输入流
DataOutputStream dos=null; //定义用来实现从客户端发送数据到服务器的输出流
Thread client=null; //定义一个客户端线程 public void connect()
{
if(soc==null)
{
try
{
soc=new Socket(InetAddress.getByName("114.227.251.23"),21036); //使用端口21036实例化一个本地套接字
System.out.println(soc); //在控制台打印实例化的结果
dis=new DataInputStream(soc.getInputStream()); //将dis指向soc的输入流
dos=new DataOutputStream(soc.getOutputStream()); //将dos指向soc的输出流
String infos=new String("INFO: "); //定义一个字符串存储发送信息,其中INFO为关键字让服务器识别为连接信息
//并将name和ip用":"分开;在服务器端用StringTokenizer类来读取数据
String userinfo=ed2.getText()+":"+InetAddress.getLocalHost().toString();
dos.writeUTF(infos+userinfo);
client=new Thread(this); //将客户端线程实例化
client.start(); //启动线程
}
catch(IOException e)
{
System.out.println("Error:"+e);
disconnect();
}
} //end of if
} public void disconnect() //客户端点击断开连接要运行的方法
{
if(soc!=null)
{
try
{
client.suspend();
dos.writeUTF("QUIT"); //用输出流发送QUIT信息通知服务器断开此次通信
soc.close(); //关闭套接字
soc=null;
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
}// end of if
}
public void send() //客户端点击发送要运行的方法
{
if(soc!=null)
{
String msgs=new String("MSG: "); //定义并实例化一个字符串存储发送的聊天信息
try
{
dos.writeUTF(msgs+ed3.getText()); //用输出流发送聊天信息
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
}
}
public void run() //线程运行方法
{
String msg=null;
while(true)
{
try{msg=dis.readUTF();} //读取从服务器传来的信息
catch(IOException e)
{
System.out.println("Error:"+e);
disconnect();
}
if (msg==null) //如果从服务器传来的信息为空则断开此次连接
{
client=null;
soc=null;
// ed5.getText().clear();
}
StringTokenizer st=new StringTokenizer(msg,":"); //用StringTokenizer类来实现读取分段字符
String keyword=st.nextToken(); //读取信息头即关键字用来识别是何种信息 if(keyword.equals("PEOPLE")) //如果是PEOPLE则是服务器发来的客户连接信息
//主要用来刷新客户端的用户列表
{
// list1.clear();
while(st.hasMoreTokens()) //遍历st取得目前所连接的客户
{
String str=st.nextToken();
// ed5.getText();
}
}
else if(keyword.equals("MSG")) //如果关键字是MSG则是服务器传来的聊天信息
//主要用来刷新客户端聊天信息区将每个客户的聊天内容显示出来
{
String usr=st.nextToken();
ed4.append(usr);
ed4.append(st.nextToken("\0"));
ed4.append("\n\n");
}
else if(keyword.equals("QUIT")) //如果关键字是QUIT则是服务器关闭的信息
//用来切断此次连接
{
System.out.println("Quit");
try
{
client.stop();
client=null;
soc.close();
soc=null;
}catch(IOException e)
{
System.out.println("Error:"+e);
}
// ed5.getText().clear();
}
}
} //end of run method
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.button_connect)
{
connect();
}
if(v.getId()==R.id.button_exit)
{
disconnect();
}
if(v.getId()==R.id.button_send)
{
send();
ed3.setText("");
}
}
}
android虚拟机客户端代码package a.b;import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.StringTokenizer;import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;public class ChatClientActivity extends Activity implements OnClickListener, Runnable {
private EditText ed1;
private EditText ed2;
private EditText ed3;
private EditText ed4;
private EditText ed5;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View button_connect = findViewById(R.id.button_connect);
button_connect.setOnClickListener(this);
View button_exit = findViewById(R.id.button_exit);
button_exit.setOnClickListener(this);
View button_send = findViewById(R.id.button_send);
button_send.setOnClickListener(this);
EditText ed1= (EditText) findViewById(R.id.editText_serveraddress);
EditText ed2 = (EditText) findViewById(R.id.editText_carname);
EditText ed3 = (EditText) findViewById(R.id.editText_content);
EditText ed4 = (EditText) findViewById(R.id.editText_showcontent);
EditText ed5 = (EditText) findViewById(R.id.editText_showname);
// ed1.setText("localhost");
}
Socket soc=null; //定义连接套接字
DataInputStream dis=null; //定义用来实现客户端接受服务器数据的输入流
DataOutputStream dos=null; //定义用来实现从客户端发送数据到服务器的输出流
Thread client=null; //定义一个客户端线程 public void connect()
{
if(soc==null)
{
try
{
soc=new Socket(InetAddress.getByName("114.227.251.23"),21036); //使用端口21036实例化一个本地套接字
System.out.println(soc); //在控制台打印实例化的结果
dis=new DataInputStream(soc.getInputStream()); //将dis指向soc的输入流
dos=new DataOutputStream(soc.getOutputStream()); //将dos指向soc的输出流
String infos=new String("INFO: "); //定义一个字符串存储发送信息,其中INFO为关键字让服务器识别为连接信息
//并将name和ip用":"分开;在服务器端用StringTokenizer类来读取数据
String userinfo=ed2.getText()+":"+InetAddress.getLocalHost().toString();
dos.writeUTF(infos+userinfo);
client=new Thread(this); //将客户端线程实例化
client.start(); //启动线程
}
catch(IOException e)
{
System.out.println("Error:"+e);
disconnect();
}
} //end of if
} public void disconnect() //客户端点击断开连接要运行的方法
{
if(soc!=null)
{
try
{
client.suspend();
dos.writeUTF("QUIT"); //用输出流发送QUIT信息通知服务器断开此次通信
soc.close(); //关闭套接字
soc=null;
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
}// end of if
}
public void send() //客户端点击发送要运行的方法
{
if(soc!=null)
{
String msgs=new String("MSG: "); //定义并实例化一个字符串存储发送的聊天信息
try
{
dos.writeUTF(msgs+ed3.getText()); //用输出流发送聊天信息
}
catch(IOException e)
{
System.out.println("Error:"+e);
}
}
}
public void run() //线程运行方法
{
String msg=null;
while(true)
{
try{msg=dis.readUTF();} //读取从服务器传来的信息
catch(IOException e)
{
System.out.println("Error:"+e);
disconnect();
}
if (msg==null) //如果从服务器传来的信息为空则断开此次连接
{
client=null;
soc=null;
// ed5.getText().clear();
}
StringTokenizer st=new StringTokenizer(msg,":"); //用StringTokenizer类来实现读取分段字符
String keyword=st.nextToken(); //读取信息头即关键字用来识别是何种信息 if(keyword.equals("PEOPLE")) //如果是PEOPLE则是服务器发来的客户连接信息
//主要用来刷新客户端的用户列表
{
// list1.clear();
while(st.hasMoreTokens()) //遍历st取得目前所连接的客户
{
String str=st.nextToken();
// ed5.getText();
}
}
else if(keyword.equals("MSG")) //如果关键字是MSG则是服务器传来的聊天信息
//主要用来刷新客户端聊天信息区将每个客户的聊天内容显示出来
{
String usr=st.nextToken();
ed4.append(usr);
ed4.append(st.nextToken("\0"));
ed4.append("\n\n");
}
else if(keyword.equals("QUIT")) //如果关键字是QUIT则是服务器关闭的信息
//用来切断此次连接
{
System.out.println("Quit");
try
{
client.stop();
client=null;
soc.close();
soc=null;
}catch(IOException e)
{
System.out.println("Error:"+e);
}
// ed5.getText().clear();
}
}
} //end of run method
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.button_connect)
{
connect();
}
if(v.getId()==R.id.button_exit)
{
disconnect();
}
if(v.getId()==R.id.button_send)
{
send();
ed3.setText("");
}
}
}
解决方案 »
- 关于安卓开发工具的问题
- Android从Ubuntu里面改在win7里面签名编译出现Conversion to Dalvik format failed with error 1
- 在android层模拟一个陀螺仪事件
- 难难题啊,后台运行的程序如何切换到前台?不用notification机制
- 自定义控件问题,在布局里使用 android:visibility="gone" 无效
- Android图片拖动问题
- android打印或输出调试信息,在线等
- 关于gallery的position的到底是什么的问题~
- 在ListView中使用写setImageBitmap()方法,怎么会报错?
- android studio socket 错误
- contentprovider的安全互斥机制如何保证
- 2.3之后版本工程目录中也无混淆文件proguade.cfg的问题
public int getIP(Context ctx) {
final String sname = Context.WIFI_SERVICE;
WifiManager Mgr = (WifiManager)(ctx.getSystemService(sname));
DhcpInfo info = Mgr.getDhcpInfo();
return(info.ipAddress);
}
try{msg=dis.readUTF();} //读取从服务器传来的信息
catch(IOException e)
{
System.out.println("Error:"+e);
disconnect();
}改成:
try{msg=dis.readUTF();} //读取从服务器传来的信息
catch(IOException e)
{
System.out.println("Error:"+e);
disconnect();
}
catch(EOFException e){ System.out.println("文件已读完");}
反正是我觉得是这里的代码有问题,请大神们仔细看看,谢谢soc=new Socket(InetAddress.getByName("114.227.251.23"),21036); //使用端口21036实例化一个本地套接字
System.out.println(soc); //在控制台打印实例化的结果
dis=new DataInputStream(soc.getInputStream()); //将dis指向soc的输入流
dos=new DataOutputStream(soc.getOutputStream()); //将dos指向soc的输出流
String infos=new String("INFO: "); //定义一个字符串存储发送信息,其中INFO为关键字让服务器识别为连接信息
//并将name和ip用":"分开;在服务器端用StringTokenizer类来读取数据
String userinfo=ed2.getText()+":"+InetAddress.getLocalHost().toString();
dos.writeUTF(infos+userinfo);
client=new Thread(this); //将客户端线程实例化
client.start(); //启动线程