server:
public class main { public static void main(String[] args) {
// TODO Auto-generated method stub ServerSocket ss=null;
Socket s = null;
DataInputStream dis =null;
DataOutputStream dos = null;
//监听端口
try {
ss = new ServerSocket(1234);
} catch (IOException e) {e.printStackTrace();}
while(true){
try {
s = ss.accept();
dis =new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
System.out.println("服务端接收的信息是:"+dis.readUTF());
dos.writeUTF("我收到你的消息。土豆!");
} catch (IOException e) {e.printStackTrace();}
}
}
}Client:
public class Main extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button button;
EditText edit;
TextView text;
Socket socket;
DataInputStream dis = null;
DataOutputStream dos = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit = (EditText) findViewById(id.edit);
text = (TextView) findViewById(id.text);
button = (Button) findViewById(id.button);
button.setOnClickListener(this);
} public void onClick(View v) {
// TODO Auto-generated method stub
if(v==button){
try {
socket = new Socket("192.168.11.12",1234);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//发送信息
try {
dos.writeUTF(edit.getText().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//接收信息
try {
text.setText(dis.readUTF().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
结果是[2010-12-16 15:36:01 - Emulator] Warning: No DNS servers found!
public class main { public static void main(String[] args) {
// TODO Auto-generated method stub ServerSocket ss=null;
Socket s = null;
DataInputStream dis =null;
DataOutputStream dos = null;
//监听端口
try {
ss = new ServerSocket(1234);
} catch (IOException e) {e.printStackTrace();}
while(true){
try {
s = ss.accept();
dis =new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
System.out.println("服务端接收的信息是:"+dis.readUTF());
dos.writeUTF("我收到你的消息。土豆!");
} catch (IOException e) {e.printStackTrace();}
}
}
}Client:
public class Main extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button button;
EditText edit;
TextView text;
Socket socket;
DataInputStream dis = null;
DataOutputStream dos = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit = (EditText) findViewById(id.edit);
text = (TextView) findViewById(id.text);
button = (Button) findViewById(id.button);
button.setOnClickListener(this);
} public void onClick(View v) {
// TODO Auto-generated method stub
if(v==button){
try {
socket = new Socket("192.168.11.12",1234);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//发送信息
try {
dos.writeUTF(edit.getText().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//接收信息
try {
text.setText(dis.readUTF().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
结果是[2010-12-16 15:36:01 - Emulator] Warning: No DNS servers found!
解决方案 »
- Spinner的样式应该如何设置
- android wifi问题
- 求NAS 相关资料!!非常感谢!(androd,linux平台)
- android的朋友看过来,100分求如何实现在代码程序中创建网站的快捷方式(需要网站图标,另外本人创建了一个群(181357702),欢迎交流
- 小弟想做个会动的圆,但有问题,请指教
- Launch里如何将特定图标放在第一位
- 求证,用于Android开发的框架(类库)和Oracle-Java-JDK大部分都不一样????????google重写了大部分JDK?????????????
- android: ImageView的setImageBitmap()方法
- 请问, ADT 在什么地方可以设置项目源码编译成功后, APK输出的目录 ?
- 布局问题
- android2.2源码 装上Google Map 不能基站定位,有没对这块比较了解的
- 我的程序写出来了,无错误,但在模拟器里运行的时候提示中止了
我将我的电脑的ip设置成了192.168.11.12。
那IP应该怎么样设置呀