java TCP&UDP socket编程示例

在《计算机网络-自顶向下方法 》第四版的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();
}
}

/

/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值