import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignedObject;
public class MainSocket {
public MainSocket() {
//Transmission control protocol user datagram protocol
//DatagramSocket udp
//send close
//sock_stream sock_dgram sock_raw
//DatagramPacket
//InetAddress
try {
byte[] buf=new byte[1024];
DatagramSocket ds=new DatagramSocket();
InetAddress add=null;
try {
add = InetAddress.getByName("www.www");
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
try {
ds.send(new DatagramPacket(buf,buf.length,add,5555));
} catch (IOException e) {
e.printStackTrace();
}
DatagramPacket dp=new DatagramPacket(buf,1024);
try {
ds.receive(dp);
} catch (IOException e) {
e.printStackTrace();
}
new String(dp.getData(),0,dp.getLength());
dp.getAddress().getHostAddress();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//ServerSocket tcp
}
public void server(){
byte[] buf=new byte[1024];
InetAddress add=null;
DatagramSocket ds=null;
DatagramPacket dp=null;
try {
add = InetAddress.getByName("www.www");
dp=new DatagramPacket(buf,1024,add,5555);
ds = new DatagramSocket(5555);
ds.send(dp);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//多点传送 multi send
public void mulit(){
try {
byte[] buf=new byte[1024];
InetAddress group=InetAddress.getByName("");
MulticastSocket ms=new MulticastSocket(5555);
ms.joinGroup(group);
DatagramPacket dp=new DatagramPacket(buf,buf.length,group,5555);
ms.send(dp);
ms.leaveGroup(group);
} catch (IOException e) {
e.printStackTrace();
}
}
public void client(){
byte[] buf=new byte[1024];
DatagramSocket ds;
DatagramPacket dp=new DatagramPacket(buf,buf.length);
try {
ds = new DatagramSocket(5555);
ds.receive(dp);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) throws Exception{
Socket s=null;
ObjectOutputStream os=null;
s=new Socket("pureajava",40000);
os=new ObjectOutputStream(s.getOutputStream());
//generate public and private keys
KeyPairGenerator kgen=KeyPairGenerator.getInstance("DSA");
kgen.initialize(256);
KeyPair kpair=kgen.generateKeyPair();
//generate a signature
Signature sig=Signature.getInstance("SHA/DSA");
PublicKey pub=kpair.getPublic();
PrivateKey priv=kpair.getPrivate();
sig.initSign(priv);
//Read a file and compute a signature
FileInputStream fis =new FileInputStream("demo");
byte arr[]= new byte[fis.available()];
fis.read(arr);
sig.update(arr);
//send the signedobject on the wire
SignedObject obj=new SignedObject(arr,priv,sig);
// new SignedObject(arr,sig.sign(),pub);
os.writeObject(obj);
fis.close();
os.close();
s.close();
//service
ServerSocket service=new ServerSocket(4000);
Socket clientSocket=service.accept();
ObjectInputStream ois=new ObjectInputStream(clientSocket.getInputStream());
SignedObject ob=(SignedObject)ois.readObject();
//generate object's signature
Signature sign=Signature.getInstance("SHA/DSA");
sign.initVerify(pub);
sign.update(ob.getSignature());
//verify the signature
boolean valid=sign.verify(ob.getSignature());
}
}
分享到:
相关推荐
这篇博文“JAVA NET 发送HTTP请求”可能详细介绍了如何使用Java的内置库`java.net`来实现这一功能。`java.net`包提供了Socket和URLConnection类,可以用来建立网络连接并发送HTTP请求。 首先,我们来看`HttpUtil....
java netjava net
`javanet`标签表明我们将深入探讨Java中的网络API和相关概念。 首先,Java提供了丰富的类库来支持网络编程,如`java.net`包,它包含了Socket、ServerSocket、URL、URLConnection等核心类。Socket类是TCP/IP通信的...
Java核心编程中的`javanet`模块主要涵盖了Java在处理网络编程方面的知识。这个主题包括了网络通信的各种基本概念、API以及其实现方法。在Java中,`java.net`包提供了大量的类和接口,用于实现客户端和服务器之间的...
根据提供的文件内容,以下是详细的Java相关知识点: 1. Java软件开发基础: - Java是一种面向对象的编程语言,是软件开发领域的主流语言之一。 - 软件开发过程包括需求分析、软件设计、编写程序、测试以及维护等...
Java网络编程是Java开发者必备的一项技能,特别是在面试中,它经常作为考察重点。Java的网络编程主要涉及到Socket编程、URL连接、HTTP协议、NIO(非阻塞I/O)以及多线程等核心概念。 1. **Java Socket编程**:Java...
maven-javanet-skin-1.2.jar
maven-javanet-skin-1.1.jar
maven-javanet-skin-1.0.jar
maven-javanet-plugin-1.7.jar
maven-javanet-plugin-1.6.jar
maven-javanet-plugin-1.5.jar
maven-javanet-plugin-1.4.jar
maven-javanet-plugin-1.3.jar
maven-javanet-plugin-1.2.jar
maven-javanet-plugin-1.1.jar
java net struts struts2 hibernate spring 源代码 例子 SSH2配置 是男人绝对错不了,特别是学JAVA必备武器之一 10分不算多,绝对物超所值
maven-javanet-skin-1.2-sources.jar
maven-javanet-skin-1.1-sources.jar
maven-javanet-skin-1.0-sources.jar