我正在开发两个程序,一个是运行服务器和第二个程序的Microsoft Visual Studio 2010软件:Eclipse ADT(ANDROID设备开发环境),我认为它是客户端。目标是在C#中的SERVER和Android CLIENT之间建立连接。它不应该是一个问题,我试图在TCP和UDP尝试,它落在对象创建上下文(在落在对象套接字的TCP和UDP SOCKET落入DATAGRAM对象创建),在我看来,问题是:activitythread .performlaunchactivity(activitythread $ activityclientreco rd intent)line 2247C#服务器和Androiod客户端
据我所知,问题不在于我的代码片段,而是在Eclipse中内置SOCKET补充软件,我尝试重新安装,添加最新的插件。针对您的下一个问题,我为Android设备和互联网设置了权限。这我不知道如何处理这个问题,我打算一个月或更多..
在additiong我会在Android客户端添加我的代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.util.Log;
public class MainActivity extends Activity {
private TextView txt;
private Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)findViewById(R.id.button1);
txt = (TextView)findViewById(R.id.textView1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
connectSocket("Hello");
}
});
}
private void connectSocket(String a){
try {
InetAddress serverAddr = InetAddress.getByName("10.0.0.8");
Log.d("TCP", "C: Connecting...");
**Socket socket = new Socket(serverAddr, 4444);**
String message = "1";
PrintWriter out = null;
BufferedReader in = null;
try {
Log.d("TCP", "C: Sending: '" + message + "'");
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println(message);
while ((in.readLine()) != null) {
txt.append(in.readLine());
}
Log.d("TCP", "C: Sent.");
Log.d("TCP", "C: Done.");
} catch(Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
socket.close();
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
Log.e("TCP", "C: UnknownHostException", e);
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("TCP", "C: IOException", e);
e.printStackTrace();
}
}
}
和我在C#中的服务器:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
public class TestTCP
{
public static void Main()
{
try
{
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
// use local m/c IP address, and
// use the same in the client
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, 11000);
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("The server is running at port 11000...");
Console.WriteLine("The local End point is :" +
myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
m:
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
char cc = ' ';
string test = null;
Console.WriteLine("Recieved...");
for (int i = 0; i < k - 1; i++)
{
Console.Write(Convert.ToChar(b[i]));
cc = Convert.ToChar(b[i]);
test += cc.ToString();
}
switch (test)
{
case "1":
break;
}
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement");
/* clean up */
goto m;
s.Close();
myList.Stop();
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
谢谢。
好吧,所以在C#中我没有做任何改变,但在Android中我把权限,并将ipAdrr更改为10.0.0.8,并做了像你一样的b.click线程。现在,它不起作用,但它并没有把我踢出程序。该怎么办 ?没有错误或什么。非常感谢!! :) – 2014-09-20 16:47:26
@BenTubul必须将C#代码更改为IPAddress ipAd = IPAddress.Parse(“10.0.0.8”);'和Android应用程序到'Socket套接字=新套接字(serverAddr,11000);'(Android应用程序必须连接到你的C#服务器正在监听的端口和地址是相同的)如果你这样做,它应该可以工作(至少它适用于我)不要忘记,android应用程序和你的C#服务器之间的东西可能会放弃连接尝试。检查是否启用了可以避免连接的防火墙或防病毒程序。 – Gooseman 2014-09-20 17:00:53
正如你所看到的: http://sizmedia.com/my.php?i=m1mn2ruijnly.png 这是错误的,程序不让我运行它。 再次感谢您的帮助伙计:) – 2014-09-20 17:09:51