今天遇到在上位机和app建立socket通信时,上位机发送数据后APP接收不到的问题
上位机部分代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.IO.Ports;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace face_add
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Socket socketwatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress ip = IPAddress.Any;
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtport.Text));
socketwatch.Bind(point);
ShowMsg("监听成功");
socketwatch.Listen(10);
Thread th = new Thread(listen);
th.IsBackground = true;
th.Start(socketwatch);
}
catch { }
}
Socket socketsend;
void listen(Object o)
{
Socket socketwatch = o as Socket;
//等待客户端连接,并创建一个新的socket
try
{
while (true)
{
socketsend = socketwatch.Accept();
disocket.Add(socketsend.RemoteEndPoint.ToString(),socketsend);
cbousers.Items.Add(socketsend.RemoteEndPoint.ToString());
ShowMsg(socketsend.RemoteEndPoint.ToString() + ":" + "连接成功");
//客户端连接成功后,服务器应接收客户端发来的消息
Thread th = new Thread(receive);
th.IsBackground = true;
th.Start(socketsend);
}
}
catch { }
}
//服务器端不停的接收来自客户端的消息
void receive(object o)
{
Socket socketsend = o as Socket;
try
{
while (true)
{
byte[] buffer = new byte[1024 * 1024 * 2];
int r = socketsend.Receive(buffer);
if (r == 0) break;
string str = Encoding.UTF8.GetString(buffer, 0, r);
ShowMsg(socketsend.RemoteEndPoint + ":" + str);
}
}
catch { }
}
void ShowMsg(string str)
{
txtlog.AppendText(str + "\r\n");
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void txtlog_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
//将远程连接的客户端的ip地址和socket存入集合
Dictionary<string, Socket> disocket = new Dictionary<string, Socket>();
//服务器给客户端发送消息
private void button4_Click(object sender, EventArgs e)
{
try
{
string str = txtsend.Text;
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
string ip = cbousers.SelectedItem.ToString();
disocket[ip].Send(buffer);
//socketsend.Send(buffer);
}
catch
{ }
}
private void richTextBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
android app部分代码:(创建链接和读取部分)
public class Connect_Thread extends Thread //用线程创建Socket连接
{
public void run() {
try {
if (socket == null) //判断socket的状态,防止重复执行
{
// socket = new Socket("192.168.31.71", 2000);
// socket = new Socket("192.168.137.122", 2000);
socket = new Socket("192.168.1.105", 3306); //如果socket为空则执行新建一个socket
Log.d("nba","nba");
inputStream = socket.getInputStream();//获取socket的输入流和输出流
outputStream = socket.getOutputStream();
// outputStream.write(sendData.getBytes());
ThreadReadData t1 = new ThreadReadData();//新建一个线程读数据
t1.start();
t2.start();
}
} catch (Exception e) //如果有错误则在这里返回
{
e.printStackTrace();
}
}
}
public class ThreadReadData extends Thread //用线程读取服务器发来的数据
{
public void run() {
//定义一个变量用于储存服务器发来的数据
while (RD) //根据RD变量的值判断是否执行读数据
{
try {
final byte[] ReadBuffer = new byte[2048]; //定义一个字节集,存放输入的数据,缓存区大小为2048字节
final int ReadBufferLengh; //用于存放数据量
//从输入流获取服务器发来的数据和数据宽度
//ReadBuffer为参考变量,在这里会改变为数据
//输入流的返回值是服务器发来的数据宽度
ReadBufferLengh = inputStream.read(ReadBuffer);
//ReadBufferLengh = 10;
Log.i("Readbuffer", String.valueOf(ReadBuffer));
Log.i("ReadbufferLengh", String.valueOf(ReadBufferLengh));
if (ReadBufferLengh == -1) //验证数据宽度,如果为-1则已经断开了连接
{
RD = false;//重新归位到初始状态
socket.close();
socket = null;
Log.d(TAG, "run: 连接服务器");
} else {
//如果有数据正常返回则进行处理显示
textdata="";
textdata = new String(ReadBuffer, 0, ReadBufferLengh, "GB2312");//原始编码数据
Log.d("TCP", new String(textdata.getBytes(), "UTF-8"));//转为UTF-8编码后显示在编辑框中
if(textdata.length()>0)
{
textdata1=textdata;
}
RD = false;
sendData=null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
运行结果及报错内容
这是上位机的内容,数据已经发送
app端接收到的内容是
我尝试过的方法
尝试更改IP地址和端口进行新的数据连接,但是仍然有问题出现