套接字已关闭 - C# 服务器 Android 客户端
本文关键字:服务器 Android 客户端 套接字 | 更新日期: 2023-09-27 18:35:33
我有以下代码。我的 c# 服务器和 Android 客户端可以在单击连接按钮时无缝连接,但在连接后我无法将数据从 android 客户端发送到 c# 服务器,Android 客户端说:"套接字已关闭"
当我单击连接按钮时,android是连接并且服务器接受,但我单击发送客户端说套接字已关闭
C# 服务器
public class SoketPaketi
{
public Socket soket;
public int istemciNo;
public byte[] tampon = new byte[1024];
public SoketPaketi(Socket soc, int no)
{
this.soket = soc;
this.istemciNo = no;
}
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dinlemeyeBasla();
}
private void dinlemeyeBasla()
{
sunucuSoketi = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint IP_PORT = new IPEndPoint(IP_ADRES,PORT);
sunucuSoketi.Bind(IP_PORT);
sunucuSoketi.Listen(10);
Console.WriteLine("Server Dinlemeye Başladı.. " + IP_PORT.ToString());
sunucuSoketi.BeginAccept(new AsyncCallback(onBaglanmaIstegiGeldiginde), null);
}
private void onBaglanmaIstegiGeldiginde(IAsyncResult ar)
{
Socket calismaSoketi = sunucuSoketi.EndAccept(ar);
istemciler.Add(calismaSoketi);
veriBekle(calismaSoketi,istemciler.Count - 1);
sunucuSoketi.BeginAccept(new AsyncCallback(onBaglanmaIstegiGeldiginde), null);
Console.WriteLine("İstemci Bağlantısı Kabul Edildi..");
}
private void veriBekle(Socket socket, int istemciNo)
{
try
{
if (geriBildirim == null)
{
geriBildirim = new AsyncCallback(onVeriGeldiginde);
}
SoketPaketi soketPaketi = new SoketPaketi(socket, istemciNo);
socket.BeginReceive(soketPaketi.tampon, 0, soketPaketi.tampon.Length,SocketFlags.None, onVeriGeldiginde, soketPaketi);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
private void onVeriGeldiginde(IAsyncResult ar)
{
//Console.WriteLine("onVeriGeldiginde");
SoketPaketi soketPaketi = (SoketPaketi)ar.AsyncState;
int gelenBoyut = soketPaketi.soket.EndReceive(ar);
if (gelenBoyut > 0)
{
char[] karakterler = new char[gelenBoyut + 1];
Decoder d = Encoding.UTF8.GetDecoder();
int karakterUzunlugu = d.GetChars(soketPaketi.tampon, 0, gelenBoyut, karakterler, 0);
String gelenVeri = new String(karakterler);
Console.WriteLine("İstemciden Veri Geldi.. Gelen Veri : " + gelenVeri);
Console.WriteLine("Data Recieved from client.. Received Data : " + gelenVeri);
}
veriBekle(soketPaketi.soket, soketPaketi.istemciNo);
}
}
}
安卓客户端
/*********************** Bağlan *********************/
/*********************** Connect *********************/
public class Baglan extends AsyncTask<Void,Void,Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Sunucuya Bağlanılıyor..!");
progressDialog.show();
}
@Override
protected Void doInBackground(Void... args) {
InetAddress serverAddr = null;
try {
serverAddr = InetAddress.getByName(Evrensel.SERVER_IP);
socket = new Socket(serverAddr, Evrensel.SERVER_PORT);
Log.d("FEF","Evrensel Socket bağlandı.." + socket.isConnected());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
socket = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(Void v) {
super.onPostExecute(v);
progressDialog.dismiss();
}
}
/*********************** Send Message *********************/
/*********************** Mesaj Gönder *********************/
public class MesajGonder extends AsyncTask <String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Sunucuya Mesaj Gönderiliyor..!");
progressDialog.show();
}
@Override
protected String doInBackground(String... args) {
String serverResponse = "";
PrintWriter mBufferOut = null;
try {
mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
mBufferOut.println(args[0]);
} catch (IOException e) {
e.printStackTrace();
}
return serverResponse;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
}
}
我解决了这个问题。问题是异步任务。不要使用 asynctask..哎呀..使用线程..可运行线程..
新的安卓客户端代码是..
public class MainActivity extends ActionBarActivity {
public static Socket socket;
public static final String SERVER_IP = "192.168.1.81";
public static final int SERVER_PORT = 6000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnBaglan = (Button)findViewById(R.id.btnBaglan);
btnBaglan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Baglan()).start();
}
});
Button btnGonder = (Button)findViewById(R.id.btnGonder);
btnGonder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new MesajGonder()).start();
}
});
}
public class Baglan implements Runnable {
@Override
public void run() {
InetAddress serverAddr = null;
try {
serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVER_PORT);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.d("FEF", "Socket bağlandımı:" + socket.isConnected());
}
}
public class MesajGonder implements Runnable{
@Override
public void run() {
PrintWriter mBufferOut = null;
try {
mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
mBufferOut.println("Breko breko");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}