Java Socket Programming for Client-Server Communication
Classified in Computers
Written on in
English with a size of 2.62 KB
Java Socket Implementation
Client-Server Socket Class (`Misocket`)
Constructor for Server (`Misocket(int port)`):
- Initializes a `ServerSocket` with the specified port.
- Waits for and accepts a client connection using `serverSocket.accept()`.
- Obtains input and output streams from the accepted client socket.
Constructor for Client (`Misocket(String ip, int port)`):
- Creates a `Socket` to connect to the specified IP address and port.
- Obtains input and output streams for communication.
Core Functionality
Sending and Receiving Strings:
- `dos.writeUTF(cadena); dos.flush();` to send a string.
- `cadena = dis.readUTF(); return cadena;` to receive a string.
Sending and Receiving Integers:
- `dos.writeInt(tam);` to send an integer.
- `tam = dis.readInt(); return tam;` to receive