public class SimpleServer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建一个ServerSocket,用于监听客户端socket的连接请求
ServerSocket ss=new ServerSocket(50069);
//采用循环不断接受来自客户端的请求,服务器端也对应产生一个Socket
while(true){
Socket s = ss.accept();
//接收客户端消息
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println("-------------------------");
String tt = br.readLine();
String res = "server 回复了你的"+tt;
System.out.println(res);
OutputStream os=s.getOutputStream();
os.write(res.getBytes("UTF-8"));
System.out.println("-------------------------");
//os.close();
s.shutdownOutput();
s.close();
}
}}
public class MainActivity extends Activity implements Runnable{
EditText show;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = (EditText) findViewById(R.id.edit1);
MainActivity myThread = new MainActivity();
Thread thread = new Thread(myThread);
thread.start();
}
@Override
public void run() {
try {
Socket socket = new Socket("192.168.145.96", 50069);
//设置10秒之后即认为是超时
socket.setSoTimeout(10000);
//发送数据给服务端
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello,server".getBytes("UTF-8"));
socket.shutdownOutput();
//读取数据
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = br.readLine();
//打印读取到的数据
Log.e("MainActivity", ">>>>>>>>>>>>>>>>>>>>>>>>>" + line);
br.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
Log.e("UnknownHost", "来自服务器的数据");
e.printStackTrace();
} catch (IOException e) {
Log.e("IOException", "来自服务器的数据");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有