mars老师[Android开发视频教学]01_24 Socket编程

时间:2022-01-06 04:00:12

mars老师[Android开发视频教学]01_24 Socket编程 源码

 

mars老师[Android开发视频教学]01_24 Socket编程   mars老师[Android开发视频教学]01_24 Socket编程

 

mars老师[Android开发视频教学]01_24 Socket编程          mars老师[Android开发视频教学]01_24 Socket编程

 

 

创建两个项目:Socket、Client

Socket为服务器端, Client为客户端

==>SocketActivity.java

package mars.socket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SocketActivity extends Activity {
/** Called when the activity is first created. */
private Button startButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button)findViewById(R.id.startListener);
startButton.setOnClickListener(new StartSocketListener());
}

class StartSocketListener implements OnClickListener{

@Override
public void onClick(View v) {
new ServerThread().start();
}

}

class ServerThread extends Thread{
//使用TCP协议客户端所要进行的操作
/*public void run(){
//声明一个ServerSocket对象
ServerSocket serverSocket = null;
try {
//创建一个ServerSocket对象,并让这个Socket在4567端口监听
serverSocket = new ServerSocket(4567);
//调用ServerSocket的accept()方法,接受客户端所发送的请求
Socket socket = serverSocket.accept();
//从Socket当中得到InputStream对象
InputStream inputStream = socket.getInputStream();
byte buffer [] = new byte[1024*4];
int temp = 0;
//从InputStream当中读取客户端所发送的数据
while((temp = inputStream.read(buffer)) != -1){
System.out.println(new String(buffer,0,temp));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}*/
//使用UDP协议客户端所要进行的操作
public void run(){
try {
//创建一个DatagramSocket对象,并指定监听的端口号
DatagramSocket socket = new DatagramSocket(4567);
byte data [] = new byte[1024];
//创建一个空的DatagramPacket对象
DatagramPacket packet = new DatagramPacket(data,data.length);
//使用receive方法接收客户端所发送的数据
socket.receive(packet);
String result = new String(packet.getData(),packet.getOffset(),packet.getLength());
System.out.println("result--->" + result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


Client项目

使用TCP协议

==>TCPClient.java

package mars;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;

public class TCPClient {
public static void main(String[] args) {
try {
//创建一个Socket对象,指定服务器端的IP地址和端口号
Socket socket = new Socket("192.168.1.104",4567);
//使用InputStream读取硬盘上的文件
InputStream inputStream = new FileInputStream("f://file/words.txt");
//从Socket当中得到OutputStream
OutputStream outputStream = socket.getOutputStream();
byte buffer [] = new byte[4*1024];
int temp = 0 ;
//将InputStream当中的数据取出,并写入到OutputStream当中
while((temp = inputStream.read(buffer)) != -1){
outputStream.write(buffer, 0, temp);
}
outputStream.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


使用UDP协议

==>UDPClient.java

package mars;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPClient {
public static void main(String[] args) {
try {
//首先创建一个DatagramSocket对象
DatagramSocket socket = new DatagramSocket(4567);
//创建一个InetAddree
InetAddress serverAddress = InetAddress.getByName("183.30.189.14");
String str = "hello";
byte data [] = str.getBytes();
//创建一个DatagramPacket对象,并指定要讲这个数据包发送到网络当中的哪个地址,以及端口号
DatagramPacket packet = new DatagramPacket(data,data.length,serverAddress,4567);
//调用socket对象的send方法,发送数据
socket.send(packet);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}