Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- import javax.xml.crypto.Data;
- import java.io.*;
- import java.util.*;
- import java.net.*;
- class TCPServer
- {
- private ServerSocket serverSocket;
- public TCPServer() throws IOException {
- serverSocket=new ServerSocket(9876);
- }
- public InetAddress getAdress(){return serverSocket.getInetAddress();}
- public int getPort(){return serverSocket.getLocalPort();}
- public void listen() throws IOException {
- String data=null;
- Socket client=serverSocket.accept();
- String clientAdress=client.getInetAddress().getHostAddress();
- int localPort=client.getLocalPort();
- System.out.println("Established connection with "+clientAdress);
- DataInputStream inputStream=null;
- try{
- inputStream=new DataInputStream(new BufferedInputStream(
- client.getInputStream()
- ));
- System.out.println(inputStream.readDouble());
- System.out.println(inputStream.readLong());
- System.out.println(inputStream.readBoolean());
- System.out.println(inputStream.readUTF());
- }
- finally {
- if(inputStream!=null)
- inputStream.close();
- }
- }
- }
- class TCPClient
- {
- private Socket server;
- private DataOutputStream out;
- TCPClient(InetAddress serverAdress, int serverPort) throws IOException {
- server=new Socket(serverAdress,serverPort);
- out=null;
- }
- public void start() throws IOException {
- try{
- out=new DataOutputStream(server.getOutputStream());
- out.writeDouble(1.25);
- out.writeLong(123584124);
- out.writeBoolean(true);
- out.writeUTF("UTF String");
- out.flush();
- } catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- if(out!=null)
- out.close();
- }
- }
- }
- public class Connection{
- public static void main(String[] args) throws IOException {
- TCPServer tcpServer=new TCPServer();
- TCPClient tcpClient=new TCPClient(tcpServer.getAdress(),tcpServer.getPort());
- tcpServer.listen();
- tcpClient.start();
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement