2.3.3 依葫芦画瓢,服务器没收到信息,AS的信息量太大,退无可退,请求火力支援
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.DownloadListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class welcome extends AppCompatActivity {
private TextView tv;
private Button myBtn;
private InetAddress serverAddress = null;
private DatagramSocket socket = null;
private int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
try {
socket = new DatagramSocket(42003);
serverAddress = InetAddress.getByName("10.0.2.2");
} catch (Exception e1) {
e1.printStackTrace();
}
tv = (TextView) findViewById(R.id.textView);
myBtn = (Button) findViewById(R.id.button);
tv.setText("Hello JAVA!");
myBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
System.out.println("click..");
String str = "hello";
i++;
switch (i) {
case 1:
break;
case 2:
str = "HELLO";
i = 0;
break;
}
byte data[] = str.getBytes();
tv.setText(str);
// 创建一个DatagramPacket对象,并指定要讲这个数据包发送到网络当中的哪个地址,以及端口号
DatagramPacket packet = new DatagramPacket(data,
data.length, serverAddress, 42003);
// 调用socket对象的send方法,发送数据
socket.send(packet);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.DownloadListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class welcome extends AppCompatActivity {
private TextView tv;
private Button myBtn;
private InetAddress serverAddress = null;
private DatagramSocket socket = null;
private int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
try {
socket = new DatagramSocket(42003);
serverAddress = InetAddress.getByName("10.0.2.2");
} catch (Exception e1) {
e1.printStackTrace();
}
tv = (TextView) findViewById(R.id.textView);
myBtn = (Button) findViewById(R.id.button);
tv.setText("Hello JAVA!");
myBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
System.out.println("click..");
String str = "hello";
i++;
switch (i) {
case 1:
break;
case 2:
str = "HELLO";
i = 0;
break;
}
byte data[] = str.getBytes();
tv.setText(str);
// 创建一个DatagramPacket对象,并指定要讲这个数据包发送到网络当中的哪个地址,以及端口号
DatagramPacket packet = new DatagramPacket(data,
data.length, serverAddress, 42003);
// 调用socket对象的send方法,发送数据
socket.send(packet);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}