在《计算机网络-自顶向下方法 》第四版的2.7 2.8两节的TCP和UDP套接字编程练习中有下面的源码:
< 一>UDP socket
package UDP;
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
BufferedReader infromuser = new BufferedReader(new InputStreamReader(System.in));//定义一个缓冲字符输入流,以子结转字符为参数构造
DatagramSocket clientSocket = new DatagramSocket();//创建套接字
InetAddress ipaddress = InetAddress.getByName("localhost");//获取主机ip地址
byte[] sendData = new byte[1024];
byte[] recievedData = new byte[1024];
String sentence = infromuser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendpacket = new DatagramPacket(sendData,sendData.length,ipaddress,9876);//生成数据包
clientSocket.send(sendpacket);
DatagramPacket recievedpacket = new DatagramPacket(recievedData,recievedData.length);//从主机接受数据包
clientSocket.receive(recievedpacket);
String modifiedsentence = new String(recievedpacket.getData());
System.out.println(modifiedsentence);
clientSocket.close();
}
}
package UDP;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
DatagramSocket serversocket = new DatagramSocket(9876);
byte[] recievedata = new byte[1024];
byte[] senddata = new byte[1024];
while(true)
{
DatagramPacket recievedpPacket = new DatagramPacket(recievedata,recievedata.length);
serversocket.receive(recievedpPacket);
String sentence = new String(recievedpPacket.getData());
InetAddress ipaddress = recievedpPacket.getAddress();
int port = recievedpPacket.getPort();
String capitalizedsentence = sentence.toUpperCase();
senddata = capitalizedsentence.getBytes();
DatagramPacket sendpacket = new DatagramPacket(senddata, senddata.length,ipaddress,port);
serversocket.send(sendpacket);
}
}
}
< 二> TCP socket
package socket;
import java.io.*;
import java.net.*;
public class Server {
public static void main(String[] args) throws IOException {
String clientSentence;
String capitalizedSentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while (true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader infromclient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
System.out.println("accept \n");
DataOutputStream outToclient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence =infromclient.readLine();
System.out.println("get client string "+ clientSentence+"\n");
capitalizedSentence = clientSentence.toUpperCase()+"\n";
outToclient.writeBytes(capitalizedSentence);
}
}
}
package socket;
import java.io.*;
import java.net.*;
public class CLient0 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String hostname = "localhost";
int portNUM = 6789;
String sentence ;
String modifiedStentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientsocket = new Socket(hostname,portNUM);
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
DataOutputStream outToserver = new DataOutputStream(clientsocket.getOutputStream());
sentence = inFromUser.readLine();
outToserver.writeBytes(sentence+"\n");
modifiedStentence = inFromServer.readLine();
System.out.println("FROM SERVER:"+modifiedStentence);
clientsocket.close();
}
}
/
/