当前位置:编程学习 > wap >>

Address family not supported by protocol

客户端异常:
01-05 23:06:01.484: W/System.err(3265): java.net.SocketException: socket failed: EAFNOSUPPORT (Address family not supported by protocol)
01-05 23:06:01.484: W/System.err(3265):  at libcore.io.IoBridge.socket(IoBridge.java:573)
01-05 23:06:01.484: W/System.err(3265):  at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
01-05 23:06:01.484: W/System.err(3265):  at java.net.Socket.startupSocket(Socket.java:559)
01-05 23:06:01.484: W/System.err(3265):  at java.net.Socket.tryAllAddresses(Socket.java:127)
01-05 23:06:01.484: W/System.err(3265):  at java.net.Socket.<init>(Socket.java:177)
01-05 23:06:01.484: W/System.err(3265):  at java.net.Socket.<init>(Socket.java:149)
01-05 23:06:01.484: W/System.err(3265):  at com.client.MainActivity$1.run(MainActivity.java:66)
01-05 23:06:01.484: W/System.err(3265):  at java.lang.Thread.run(Thread.java:856)
01-05 23:06:01.484: W/System.err(3265): Caused by: libcore.io.ErrnoException: socket failed: EAFNOSUPPORT (Address family not supported by protocol)
01-05 23:06:01.484: W/System.err(3265):  at libcore.io.Posix.socket(Native Method)
01-05 23:06:01.484: W/System.err(3265):  at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
01-05 23:06:01.484: W/System.err(3265):  at libcore.io.IoBridge.socket(IoBridge.java:558)
01-05 23:06:01.484: W/System.err(3265):  ... 7 more
客户端代码:
public class MainActivity extends Activity implements OnClickListener{
private static final String TAG = "SBSF";

private TextView textMsg;
private Button buttonSend;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
textMsg = (TextView) findViewById(R.id.textMsg);
buttonSend =(Button) findViewById(R.id.buttonSend);

buttonSend.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonSend:
sendMsg();
break;
}
}

private void sendMsg() {
sendThread.start();
}


private Thread sendThread = new Thread(new Runnable() {
@Override
public void run() {
try {
Socket clientSocket = new Socket("192.168.1.112", 8888);
DataOutputStream dataOutputStream = new DataOutputStream(clientSocket.getOutputStream());
dataOutputStream.writeUTF(TAG);//输出信息给服务器
DataInputStream dataInputStream = new DataInputStream(clientSocket.getInputStream());
Message message = Message.obtain();
message.obj = dataInputStream.readUTF();//输入信息显示在客户端
sendHandler.sendMessage(message);
clientSocket.close();
dataInputStream.close();
dataOutputStream.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});

private Handler sendHandler = new Handler(){

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
textMsg.setText(msg.obj.toString());
}

};
}
服务端代码:

public class ServerMain {

/**
 * @param args
 */
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8888);
Socket clientSocket = serverSocket.accept();
DataInputStream dataInputStream = new DataInputStream(clientSocket.getInputStream());
System.out.println("我来自客户端:"+dataInputStream.readUTF());
DataOutputStream dataOutputStream = new DataOutputStream(clientSocket.getOutputStream());
dataOutputStream.writeUTF("我来自服务端!");
serverSocket.close();
dataInputStream.close();
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

在有些Android设备上运行正常,有些又出现如题所示异常,跪求分析原因! socket protocol
补充:移动开发 ,  Android
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,