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!
解决方案 »
- android apk 调用另一个apk 就像Java里头调用另一个exe一样
- 横屏进入彩信,然后竖屏,actionbar显示异常
- 关于解锁屏幕后引发的问题
- android在edittext里怎么截取KEY事件(event)?
- ExpandableListActivity向下拖动出现一片空白,怎么禁止
- httpPost连接网络发送多条数据异常
- android新手求入职场的android大牛手机适配问题
- android开发,运行APP自动调用本月日历,并在日历里把本日换成图标
- 如何在手机上实现3D模型的加载和显示
- android studio 调试老是说 am start -n 错误什么原因啊,求大神指教
- android2.2源码 装上Google Map 不能基站定位,有没对这块比较了解的
- 我的程序写出来了,无错误,但在模拟器里运行的时候提示中止了
我将我的电脑的ip设置成了192.168.11.12。
那IP应该怎么样设置呀