第一次发送数据时候,电脑能接受到数据!!
但是第二次发的时候,电脑的那个客户端程序就卡住了,纳闷啊,
直到我关闭服务端的时候,它才出现连接失败,要不一直挂着
我没有关闭socket啊
不知道哪里出问题:
关键代码:
手机:服务端->MainActivity:package hua.tcpsocket;import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener; 
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;public class MainActivity extends Activity {
private boolean thread = false;
private TcpSocketServer tss = null;
private ServerThread serverThread = null;
private String buffer = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thread = false;
Button buttonStartWifi = (Button) this.findViewById(R.id.startWifi);
buttonStartWifi.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//获取字符串缓冲区buffer
EditText bufferEditText = (EditText)MainActivity.this.findViewById(R.id.buffer);
//获取端口port
EditText portEditText = (EditText)MainActivity.this.findViewById(R.id.port);
buffer = bufferEditText.getText().toString();
String port = portEditText.getText().toString();  
if(serverThread == null){
serverThread = new ServerThread("1821");
serverThread .start();
}
serverThread.sendMsg();

//提示信息
Toast.makeText(MainActivity.this, "wifi",Toast.LENGTH_SHORT).show();
}});
}
class ServerThread extends Thread{
private String port;
public ServerThread (){

}
public ServerThread (String port){
this.port = port;
}
public void run(){
//建立服务端
if(tss == null)
tss = new TcpSocketServer();
try {
//开始发射wifi信号
if(thread == false){
tss.sendMessage(Integer.parseInt(port),buffer);
thread = true;
}

} catch (Exception e) {
e.printStackTrace();
}
}
public void sendMsg(){
try {
tss.sendMessage(Integer.parseInt(port),buffer);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}手机:服务端->TcpSocketServer:
package hua.tcpsocket;import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;public class TcpSocketServer {
private ServerSocket ss =null;
private Socket s = null;
public TcpSocketServer(){
//新建ServerSocket对象,端口为传进来的port;
if(ss == null)
try {
ss= new ServerSocket(1821);
} catch (IOException e) {
e.printStackTrace();
}
//新建Socket通信对象,接受客户端发来的请求accept();
 if(s == null)
try {
s = ss.accept();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(int port,String buffer)throws Exception{
// s = ss.accept();
//创建输入流对象InputStream
InputStream bais = new ByteArrayInputStream(buffer.getBytes());
//创建输出流对象OutputStream
OutputStream out = s.getOutputStream();
byte[] buff = new byte[1024];
while(bais.read(buff) > 0){
out.write(buff);
}
// s.close();
}
}
电脑:客户端->TcpClientDemo
import java.net.*;
import java.io.*;
class TcpClientDemo 
{
public static void main(String[] args)throws Exception
{
Socket s = new Socket("192.168.228.101",1821);
InputStream is = s.getInputStream();
byte[] buff = new byte[1024];
while(is.read(buff) > 0){
System.out.println(new String(buff,0,buff.length));
}
s.close();
}}