android在线程中socket连接例子
2个回答
展开全部
package com.cpa.uri;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class ClientDemo {
public static void main(String[] args) throws IOException, Exception {
ClientDemo client=new ClientDemo();
client.open();
}
private void open() throws Exception, IOException{
Socket s=new Socket("localhost",8000);
InputStream in=s.getInputStream();
OutputStream out=s.getOutputStream();
new Reader(out).start();
new Write(in).start();
}
class Reader extends Thread{
OutputStream out;
public Reader(OutputStream out) {
this.out=out;
setDaemon(true);
}
@Override
public void run() {
try {
Scanner s=new Scanner(System.in);
while(true){
String str=s.nextLine();//读取控制台
out.write(str.getBytes());//发送到服务器
out.write('\n');
out.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.run();
}
}
class Write extends Thread{
InputStream in;
public Write(InputStream in) {
this.in=in;
}
@Override
public void run() {
int b;
try {
while((b=in.read())!=-1){
System.out.println(b);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
**************************
package com.cpa.uri;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ServerDemo {
public static void main(String[] args) throws Exception {
ServerDemo server=new ServerDemo();
server.start();
}
private void start() throws Exception{
ServerSocket ss=new ServerSocket(8000);
while (true) {
System.out.println("等待客户端连接!");
Socket s=ss.accept();
System.out.println("客户端连接成功:"+s.getInetAddress());
//为这个客户端创建一个服务线程
new Service(s).start();
}
}
class Service extends Thread{
Socket s;
public Service(Socket s){
this.s=s;
}
@Override
public void run() {
try {
InputStream in=s.getInputStream();
OutputStream out=s.getOutputStream();
//服务器向客户写消息
out.write("说口令".getBytes());
out.flush();
Scanner s=new Scanner(in);
while(true){
String str=s.nextLine();
if (str.equals("A friend in need is a friend indeed")) {
out.write("回答正确".getBytes());
out.flush();
}else if (str.equals("ni是不是笨蛋")) {
out.write("是".getBytes());
out.flush();
break;
}else{
out.write("What do you say!".getBytes());
out.flush();
}
}
s.close();
} catch (Exception e) {
}
super.run();
}
}
}
************************
客户端和服务器端java代码,自己改动下就可以变成android代码了.
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
public class ClientDemo {
public static void main(String[] args) throws IOException, Exception {
ClientDemo client=new ClientDemo();
client.open();
}
private void open() throws Exception, IOException{
Socket s=new Socket("localhost",8000);
InputStream in=s.getInputStream();
OutputStream out=s.getOutputStream();
new Reader(out).start();
new Write(in).start();
}
class Reader extends Thread{
OutputStream out;
public Reader(OutputStream out) {
this.out=out;
setDaemon(true);
}
@Override
public void run() {
try {
Scanner s=new Scanner(System.in);
while(true){
String str=s.nextLine();//读取控制台
out.write(str.getBytes());//发送到服务器
out.write('\n');
out.flush();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.run();
}
}
class Write extends Thread{
InputStream in;
public Write(InputStream in) {
this.in=in;
}
@Override
public void run() {
int b;
try {
while((b=in.read())!=-1){
System.out.println(b);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
**************************
package com.cpa.uri;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
public class ServerDemo {
public static void main(String[] args) throws Exception {
ServerDemo server=new ServerDemo();
server.start();
}
private void start() throws Exception{
ServerSocket ss=new ServerSocket(8000);
while (true) {
System.out.println("等待客户端连接!");
Socket s=ss.accept();
System.out.println("客户端连接成功:"+s.getInetAddress());
//为这个客户端创建一个服务线程
new Service(s).start();
}
}
class Service extends Thread{
Socket s;
public Service(Socket s){
this.s=s;
}
@Override
public void run() {
try {
InputStream in=s.getInputStream();
OutputStream out=s.getOutputStream();
//服务器向客户写消息
out.write("说口令".getBytes());
out.flush();
Scanner s=new Scanner(in);
while(true){
String str=s.nextLine();
if (str.equals("A friend in need is a friend indeed")) {
out.write("回答正确".getBytes());
out.flush();
}else if (str.equals("ni是不是笨蛋")) {
out.write("是".getBytes());
out.flush();
break;
}else{
out.write("What do you say!".getBytes());
out.flush();
}
}
s.close();
} catch (Exception e) {
}
super.run();
}
}
}
************************
客户端和服务器端java代码,自己改动下就可以变成android代码了.
追问
可不可以表示创建socket就会崩溃
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询