我正在開發兩個程序,一個是運行服務器和第二個程序的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