android studio 52 mp3下载客户端001
程序员文章站
2022-03-10 22:44:14
第一步:mainActivity.javapackage com.example.mydownloadmusic01;import android.annotation.SuppressLint;import android.os.Handler;import android.os.Message;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.wid...
第一步:mainActivity.java
package com.example.mydownloadmusic01;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class MainActivity extends AppCompatActivity {
TextView tv;
Button btn;
EditText edt;
String NeedFindWords;
private final int HANDLER_MSG_TELL_RECV = 0x124;
@SuppressLint("HandlerLeak")
Handler handler = new Handler(){
public void handleMessage(Message msg){
//接受到服务器信息时执行
// Toast.makeText(MainActivity.this,(msg.obj).toString(),Toast.LENGTH_LONG).show();
tv.setText((msg.obj).toString());
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.button);
edt=(EditText)findViewById(R.id.editText);
tv= (TextView) findViewById(R.id.ReData);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 响应事件
NeedFindWords=edt.getText().toString();
startNetThread();
}
});
}
private void startNetThread() {
new Thread() {
@Override
public void run() {
try {
Socket socket = new Socket("192.168.1.7", 666);
InputStream is = socket.getInputStream();
OutputStream out = socket.getOutputStream();
out.write(NeedFindWords.getBytes()); //3.发送
out.flush();
byte[] bytes = new byte[1024];
int n = is.read(bytes);
Message msg = handler.obtainMessage(HANDLER_MSG_TELL_RECV, new String(bytes, 0, n));
msg.sendToTarget();
is.close();
socket.close();
} catch (Exception e) {
}
}
}.start();
}
}
第二步 activity-main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="301dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
tools:layout_editor_absoluteX="10dp"
tools:layout_editor_absoluteY="11dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Button"
tools:layout_editor_absoluteX="264dp"
tools:layout_editor_absoluteY="8dp" />
<TextView
android:id="@+id/ReData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="7dp"
android:layout_marginEnd="3dp"
android:layout_marginRight="3dp"
android:text="Hello World!"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="98dp" />
</RelativeLayout>
import socket
import sys
from time import ctime
def FindMusicName(MusicName):
f = open('musicName3.txt','r',encoding='utf8')
for lines in f.readlines():
if lines.find("马桃")!=-1:
print(lines)
print("find it .........................")
return lines
# 1.socket(socket_family, socket_type, protocol=0)
# 其中,socket_family 是 AF_UNIX 或 AF_INET,ocket_type 是 SOCK_STREAM或 SOCK_DGRAM, protocol 通常省略,默认为 0。
# 为了创建 TCP/IP 套接字,可以用下面的方式调用 socket.socket()。
# tcpSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 同样,为了创建 UDP/IP 套接字,需要执行以下语句。
# udpSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 获取本地机器名
host = socket.gethostname()
# 设置端口
port = 666
# 2.s.bind绑定本地地址到socket对象
ServerSocket.bind((host, port))
# 3.s.listen监听地址端口,连接几个客户端
ServerSocket.listen(2)
while True:
# 4.s.accept阻塞接受链接请求,被动接受 TCP 客户端连接,一直等待直到连接到达(阻塞)
# accept()方法会返回一个含有两个元素的元组(fd,addr)。
# 第一个元素是新的socket对象,服务器通过它与客户端通信。
# 第二个元素也是元组,是客户端的地址及端口信息。
clientsocket, addr = ServerSocket.accept()
print("连接地址:%s" % str(addr))
msg = "welcomt to my demo"
all_line=""
#send()和recv()的数据格式都是bytes。
# (str和bytes的相互转化,用encode()和decode(),或者用bytes()和str())
print("send msg:welcomt to my demo.")
#clientsocket.send(msg.encode("utf-8"))
data = clientsocket.recv(1024)
print(data.decode("utf-8"))
print("显示文件歌曲查询结果")
f = open('musicName3.txt','r',encoding='utf8')
for lines in f.readlines():
if lines.find(data.decode("utf-8"))!=-1:
print(lines)
print("find it .........................")
all_line=all_line+lines;
continue
data2= all_line.encode("utf-8")
print(all_line)
#data1 = ('[%s] %s' % (ctime(),data.decode())).encode("utf-8")
clientsocket.send(data2)
#clientsocket.send(str(MusicN).decode("utf-8"))
clientsocket.close()
ServerSocket.close()
本文地址:https://blog.csdn.net/weixin_33595571/article/details/107883124
上一篇: Python实现并查集
下一篇: 自定义Radio样式配合ListView