socket点对点通信(基于模拟器)
来源:互联网 发布:俄罗斯4g网络制式 编辑:程序博客网 时间:2024/06/05 06:42
socket通信是有关服务器与客户端之间的通信,要实现点对点通信,记得抓住唯一标志,我在这里是获得访问服务端的客户端ip地址,保存在map中,然后通过ip地址过滤一些没必要接受的信息。具体实现方式如下:
首先得建立好服务端:
public class SimpleServer
{
public static ArrayList<Socket> socketList = new ArrayList<Socket>();
public static ArrayList<Map<String, Socket>> arrayList = new ArrayList<Map<String,Socket>>();
public static Map<String, Socket> map = new HashMap<String, Socket>();
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
ServerSocket ss = new ServerSocket(30000);
while(true){
Socket s = ss.accept();
InetAddress in = s.getInetAddress();
String inetAddress = in.getHostAddress();
map.put(in.getHostAddress(), s);//存放客户端访问服务的ip地址
socketList.add(s, inetAddress);// 存放客户端访问服务器的socket
new Thread(new ServerThread(s)).start();
}
}
}
服务端线程:
public class ServerThread implements Runnable
{
private Socket s = null;
private BufferedReader br = null;
public ServerThread(Socket s) throws IOException
{
this.s = s;
if(!inetAddress.equals("170.81.304.112")&&!inetAddress.equals("170.81.304.47")){// 注意:这里的ip为自己和对方的ip地址,下同
br = null;
}
else {
br = new BufferedReader(new InputStreamReader(s.getInputStream(), "utf-8"));
}
}
@Override
public void run()
{
// TODO Auto-generated method stub
try
{
String content = null;
while ((content = readFromClient()) != null)
{
for (Socket s : SimpleServer.socketList)
{
if(!s.getInetAddress().getHostAddress().equals("170.81.304.112")&&!s.getInetAddress().getHostAddress().equals("170.81.304.47")){
}
else{
OutputStream os = s.getOutputStream();
os.write((content + "\n").getBytes("utf-8"));
}
}
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String readFromClient()
{
if(br != null){
try
{
return br.readLine();
}
catch (IOException e)
{
// TODO Auto-generated catch block
// SimpleServer.socketList.remove(s);
}
}
return null;
}
}
客户端代码:
public class Client extends Activity
{
private EditText inputEditText = null;
private TextView content = null;
private Button sendMsg = null;
Socket s;
OutputStream os;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg)
{
// TODO Auto-generated method stub
super.handleMessage(msg);
content.append("\n"+msg.obj.toString());
Log.v("Jeny", "-----------"+content);
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.client);
initWidget();
try
{
s = new Socket("10.81.34.112", 30000);
new Thread(new ClientThread(s, handler)).start();
os = s.getOutputStream();
}
catch (UnknownHostException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
sendMsg.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
try
{
os.write((inputEditText.getText().toString()+"\r\n").getBytes("utf-8"));
inputEditText.setText("");
}
catch (UnsupportedEncodingException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
private void initWidget(){
inputEditText = (EditText) findViewById(R.id.input);
content = (TextView) findViewById(R.id.content);
sendMsg = (Button) findViewById(R.id.sendMsg);
}
}
客户端线程:
public class ClientThread implements Runnable
{
private Socket s;
private Handler handler;
BufferedReader br;
public ClientThread(Socket s, Handler handler) throws IOException{
this.s = s;
this.handler = handler;
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
}
@Override
public void run()
{
// TODO Auto-generated method stub
try
{
String content = null;
while((content = br.readLine())!= null){
Message msg = new Message();
msg.obj = content;
handler.sendMessage(msg);
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- socket点对点通信(基于模拟器)
- Java网络编程笔记(三):点对点通信(Socket基于TCP协议)
- 使用Socket编程实现p2p(点对点)通信
- 一种基于局域网的点对点语音通信
- 一种基于局域网的点对点语音通信
- 种基于局域网的点对点语音通信
- java版qq设计与制作:点对点通信 基于JFrame的界面和SOCKET多线程技术 求指正!!!
- android模拟器实现socket通信
- android网络编程 -- Socket 通信(03) 点对点Android聊天室实现(带服务器) [附源码分析]
- linux下使用socket实现点对点通信的实验(server to client)
- 点对点通信
- python socket 编程01-实现基本点对点通信
- 利用java socket和sampled实现点对点即时语音通信
- 基于域名 socket通信
- 基于Android socket通信
- Java基于UDP用Socket实现点对点聊天
- 基于TCP协议的点对点通信java程序,带图形界面
- Android Socket通信(一)--基于UDP协议通信
- SQL 优化原则(Oracle)
- 8月2日--进入was6.1的控制台
- t-sql创建外键
- C语言经典100例 - 003
- OC各种数据类型之间的转换方法
- socket点对点通信(基于模拟器)
- Lumigent Log Explorer是不是可以恢复所有用户误操作的数据?
- hdu 1232 畅通工程
- C/C++指针之一
- C/C++指针之二
- ORA-00600: internal error code, arguments: [spstp: ORACLE_HOME uid does not match euid], [504], [500
- Lucene 4.0 的重大升级内容一览
- C/C++指针之三
- Redis多客户端连接问题总结