Bir Android uygulaması (istemci) geliştiriyorum ve TCP iletişimimi kullanarak Java sunucumla bağlantı kurmasını istiyorum. basit bir program, istemci bir mesaj ve sunucu yankılanırken geri göndermek - - Ben kodu yaptıkAndroid istemcisi ve Java sunucusu TCP iletişimi
olarak aşağıda:
Sunucu kodu:
import java.net.*;
public class Server {
public static void main(String[] args) {
int nreq = 1;
try
{
ServerSocket sock = new ServerSocket (8080);
for (;;)
{
Socket newsock = sock.accept();
System.out.println("Creating thread ...");
Thread t = new ThreadHandler(newsock,nreq);
t.start();
}
}
catch (Exception e)
{
System.out.println("IO error " + e);
}
System.out.println("End!");
}
}
aynı proje dosyasında bir Konu İşleyici kodu
import java.io.*;
import java.net.*;
class ThreadHandler extends Thread {
Socket newsock;
int n;
ThreadHandler(Socket s, int v) {
newsock = s;
n = v;
}
public void run() {
try {
PrintWriter outp = new PrintWriter(newsock.getOutputStream(), true);
BufferedReader inp = new BufferedReader(new InputStreamReader(
newsock.getInputStream()));
outp.println("Hello :: enter QUIT to exit \n");
boolean more_data = true;
String line;
while (more_data) {
line = inp.readLine();
System.out.println("Message '" + line + "' echoed back to client.");
if (line == null) {
System.out.println("line = null");
more_data = false;
} else {
outp.println("From server: " + line + ". \n");
if (line.trim().equals("QUIT"))
more_data = false;
}
}
newsock.close();
System.out.println("Disconnected from client number: " + n);
} catch (Exception e) {
System.out.println("IO error " + e);
}
}
}
Ve bu istemci tarafı (Android) 'dir:
sunucusununVe bu XML dosyasıdır:
<?xml version="1.0" encoding="utf-8" ?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
<TextView android:id="@+id/tvText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your message here:" />
- <EditText android:id="@+id/etMsg" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10">
<requestFocus />
</EditText>
<TextView android:id="@+id/tvStatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Status..." android:textAppearance="?android:attr/textAppearanceSmall" />
<Button android:id="@+id/bSend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" />
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Conversation:" android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView android:id="@+id/tvConvo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" />
</LinearLayout>
bunu çalıştıran çalıştı, ancak hiçbir şey olmuyor. Düğmeyi tıklattığımda, TextView "Hello" iletisini sunucudan görüntülemiyor.
manifest.xml dosyasında 'var mı? –
diewie
Evet bir INTERNET izni var –