最近进行了小组第三阶段的考核,学习了socket网络编程,这一阶段我的收获特别大,UDP通信中,通过包的形式传输了需要传输的内容,TCP中以流的形式对数据进行了传输,
package test;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import javax.swing.JOptionPane;
public class ReceiveNewMsg implements Msg {
Name name;
String s,s_id;
int i;
static Vital chess_board=null;
public ReceiveNewMsg(Name name) {
this.name=name;
}
public ReceiveNewMsg(String s,String s_id,int i) {
this.s=s;
this.s_id=s_id;
this.i=i;
}
public void send(DatagramSocket ds, String IP, int udpPort)
{
ByteArrayOutputStream baoss = new ByteArrayOutputStream();
DataOutputStream doss = new DataOutputStream(baoss);
try{
doss.writeInt(Msg.RECEIVE);
doss.writeUTF(s);
doss.writeUTF(s_id);
doss.writeInt(i);
byte[] buf = baoss.toByteArray();
DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress(IP, udpPort));
ds.send(dp);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void parse(DataInputStream dis) {
try {
String s=dis.readUTF();
String s_id=dis.readUTF();
int i=dis.readInt();
if(s.equals(name.s))
{
if(i==0)
{
chess_board = new Vital(name);
name.jfrm.dispose();
}
if(i==1)
{
JOptionPane.showMessageDialog(null, "对方拒绝了你的请求");
}
}
}
catch (EOFException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是对数据的发送与解析
package test;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
public class Server {
public static final int TCP_PORT=30000;
public static final int UDP_PORT=6666;
private static int UDP_PORT_START=2226;
public static List<Client> clients=new ArrayList<Client>();
ServerSocket server=null;
public void server(){
new Thread(new UDPThread()).start();
try {
server = new ServerSocket(TCP_PORT);
System.out.println("服务器启动成功");
} catch (IOException e) {
System.out.println("服务器启动失败");
}
while(true){
Socket socket=null;
try {
socket = server.accept();
UDP_PORT_START++;
DataOutputStream out=new DataOutputStream(socket.getOutputStream());
out.writeInt(UDP_PORT_START);
out.flush();
DataInputStream in=new DataInputStream(socket.getInputStream());
String name = in.readUTF();
String IP=socket.getInetAddress().getHostAddress();
Client c=new Client(name,IP,UDP_PORT_START);
clients.add(c);
// System.out.println("连接成功"+socket.getInetAddress()+": "+socket.getPort()+"_______UDP: "
// + ""+UDP_PORT_START);
} catch (IOException e) {
System.out.println("服务器启动失败");
}finally{
if(socket!=null){
try {
socket.close();
socket = null;
} catch (IOException e){
System.out.println("服务器启动失败");
}
}
}
}
}
public static void main(String[] args) {
new Server().server();
}
class UDPThread implements Runnable{
byte[] buf=new byte[1024];
@SuppressWarnings("resource")
public void run() {
DatagramSocket ds =null;
while (true) {
try {
ds=new DatagramSocket(UDP_PORT);
} catch (SocketException e) {
System.out.println("服务器启动失败");
}
while (ds!=null) {
DatagramPacket dp=new DatagramPacket(buf, buf.length);
try {
ds.receive(dp);
System.out.println("一个包已经被收到");
for (int i = 0; i < clients.size(); i++)
{
Client c=clients.get(i);
dp.setSocketAddress(new InetSocketAddress(c.IP, c.udpPort));
ds.send(dp);
}
} catch (IOException e) {
System.out.println("服务器启动失败");
}
}
}
}
}
}
这是服务端的连接,包括TCP与UDP