目录
8-1 sdut-网络编程-1 TCP协议通信
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket ss = new ServerSocket(8888);
while(true)
{
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
byte[] bytes = new byte[10];
int len = 0;
StringBuffer buffer = new StringBuffer();
while((len = is.read(bytes)) != -1)
{
buffer.append(new String(bytes, 0,len));
}
System.out.println("I have receive" + buffer.toString());
OutputStream os = socket.getOutputStream();
os.write(("Message from Server is:" + buffer.toString()).getBytes());
is.close();
os.close();
socket.close();
if("end".equals(buffer.toString()))
{
break;
}
}
ss.close();
}
}
import javax.swing.plaf.InputMapUIResource;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class TCPClient {
public static void main(String[] args) throws IOException {
Scanner cin = new Scanner(System.in);
InetAddress ip = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
Socket socket = new Socket(ip, 8888);
OutputStream os = socket.getOutputStream();
String line = cin.nextLine();
os.write(line.getBytes());
socket.shutdownOutput();
InputStream is = socket.getInputStream();
int len = 0;
byte[] bytes = new byte[10];
StringBuffer buffer = new StringBuffer();
while((len = is.read(bytes)) != -1)
{
buffer.append(new String(bytes, 0, len));
}
System.out.println("Client has received:" + buffer.toString());
is.close();
os.close();
socket.close();
}
}
8-2 sdut-网络编程-2 UDP协议通信
import javax.xml.crypto.Data;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(9999);
while(true)
{
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
ds.receive(dp);
byte[] data = dp.getData();
String str = new String(data, 0, dp.getLength());
System.out.println("I have received" + str);
byte[] dataout = ("Message from Serve is:" + str).getBytes();
DatagramPacket dp1 = new DatagramPacket(dataout, dataout.length,
dp.getAddress(), dp.getPort());
ds.send(dp1);
if("end".equals(str))
break;
}
ds.close();
}
}
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
public class UDPClient {
public static void main(String[] args) throws IOException {
Scanner cin = new Scanner(System.in);
DatagramSocket ds = new DatagramSocket();
String line = cin.nextLine();
DatagramPacket dp = new DatagramPacket(line.getBytes(), line.length(),
InetAddress.getByAddress(new byte[]{127, 0, 0, 1}),
9999);
ds.send(dp);
byte[] bytes = new byte[1024];
DatagramPacket dp1 = new DatagramPacket(bytes, bytes.length);
ds.receive(dp1);
byte[] data = dp1.getData();
int len = dp1.getLength();
System.out.println("Client has received:");
System.out.println(new String(data, 0, len));
ds.close();
}
}