본문내용
id.com/apk/res/android\"
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
>
android:orientation=\"vertical\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
>
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
android:layout_marginBottom=\"10px\"
android:text=\"Enter some data and press Connect to submit to socket (server must be listening for this to work)\"
/>
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
android:layout_marginBottom=\"10px\"
/>
android:orientation=\"horizontal\"
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
>
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"Connect\"
/>
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"Send Message\"
/>
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"Disconnect\"
/>
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
android:layout_marginBottom=\"20px\"
android:text=\"Output Text\"
/>
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
android:layout_marginBottom=\"10px\"
android:textSize=\"10sp\"
/>
EchoThreadServer
import java.net.*;
import java.io.*;
public class EchoThreadServer
{
public static void main(String args[])
{
try {
ServerSocket server = new ServerSocket(8282);
System.out.println(\"Connection wait...\");
while(true)
{
Socket sock = server.accept();
EchoThread echothread = new EchoThread(sock);
System.out.println(\"accept... thread start...\");
echothread.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(\"Server EXIT\");
}
}
class EchoThread extends Thread {
private Socket sock;
public EchoThread(Socket sock)
{
this.sock = sock;
}
public void run()
{
InetAddress inetaddr = sock.getInetAddress();
System.out.println(inetaddr.getHostAddress() + \" Connection !!\");
try {
OutputStream out = sock.getOutputStream();
InputStream in = sock.getInputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = br.readLine()) != null)
{
if(line.equalsIgnoreCase(\"EXIT\"))
{
System.out.println(inetaddr.getHostAddress() + \" Message : \" + line);
pw.close();
br.close();
sock.close();
break;
}
else
{
System.out.println(inetaddr.getHostAddress() + \" Message : \" + line);
pw.println(line);
pw.flush();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e);
e.printStackTrace();
}
}
}
실행켭쳐
77
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
>
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
>
android:layout_height=\"wrap_content\"
android:layout_marginBottom=\"10px\"
android:text=\"Enter some data and press Connect to submit to socket (server must be listening for this to work)\"
/>
android:layout_height=\"wrap_content\"
android:layout_marginBottom=\"10px\"
/>
android:layout_width=\"fill_parent\"
android:layout_height=\"wrap_content\"
>
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"Connect\"
/>
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"Send Message\"
/>
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:text=\"Disconnect\"
/>
android:layout_height=\"wrap_content\"
android:layout_marginBottom=\"20px\"
android:text=\"Output Text\"
/>
android:layout_height=\"wrap_content\"
android:layout_marginBottom=\"10px\"
android:textSize=\"10sp\"
/>
EchoThreadServer
import java.net.*;
import java.io.*;
public class EchoThreadServer
{
public static void main(String args[])
{
try {
ServerSocket server = new ServerSocket(8282);
System.out.println(\"Connection wait...\");
while(true)
{
Socket sock = server.accept();
EchoThread echothread = new EchoThread(sock);
System.out.println(\"accept... thread start...\");
echothread.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(\"Server EXIT\");
}
}
class EchoThread extends Thread {
private Socket sock;
public EchoThread(Socket sock)
{
this.sock = sock;
}
public void run()
{
InetAddress inetaddr = sock.getInetAddress();
System.out.println(inetaddr.getHostAddress() + \" Connection !!\");
try {
OutputStream out = sock.getOutputStream();
InputStream in = sock.getInputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = br.readLine()) != null)
{
if(line.equalsIgnoreCase(\"EXIT\"))
{
System.out.println(inetaddr.getHostAddress() + \" Message : \" + line);
pw.close();
br.close();
sock.close();
break;
}
else
{
System.out.println(inetaddr.getHostAddress() + \" Message : \" + line);
pw.println(line);
pw.flush();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e);
e.printStackTrace();
}
}
}
실행켭쳐
77
소개글