Commit 8b056be6 authored by Gabriele Civitarese's avatar Gabriele Civitarese

Esempietto di comunicazione ProtocolBuffer su socket

parent c7004263
package researcher;
import it.ewlab.researcher.ResearcherOuterClass.Researcher;
import java.io.IOException;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 9999);
Researcher r =
Researcher.newBuilder()
.setName("Gabriele")
.setSurname("Civitarese")
.setType(Researcher.ResearcherType.POSTDOC)
.addPaper(Researcher.Paper.newBuilder().setTitle("Activity Recognition")
.setYear(2014))
.addPaper(Researcher.Paper.newBuilder().setTitle("Activity Recognition Again")
.setYear(2015)).build();
r.writeTo(s.getOutputStream());
s.close();
}
}
package researcher;
import it.ewlab.researcher.ResearcherOuterClass;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
Socket s = serverSocket.accept();
ResearcherOuterClass.Researcher r = ResearcherOuterClass.Researcher.parseFrom(s.getInputStream());
System.out.println(r);
}
}
package it.ewlab.researcher;
message Researcher {
required string name = 1;
required string surname = 2;
enum ResearcherType {
PHDSTUDENT = 0;
POSTDOC = 1;
ASSISTANTPROFESSOR = 2;
}
message Paper {
required string title = 1;
required int32 year = 2;
}
repeated Paper paper = 3;
optional ResearcherType type = 4;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment