0% found this document useful (0 votes)
32 views2 pages

Server Client TCP UDP

The document contains code for a TCP server, TCP client, UDP server, and UDP client in Java. The TCP server creates a server socket listening on port 5000, accepts a connection from a client, receives an integer from the client input stream, adds 1 to it, and writes the result to the output stream. The TCP client connects to the server on port 5000, writes the number 3, receives the response from the server and prints it. The UDP server creates a datagram socket listening on port 4160, receives a packet from the client, prints the response data, and closes the socket. The UDP client creates a datagram socket, sends a "Hello world" string to the server on port 4160, and closes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views2 pages

Server Client TCP UDP

The document contains code for a TCP server, TCP client, UDP server, and UDP client in Java. The TCP server creates a server socket listening on port 5000, accepts a connection from a client, receives an integer from the client input stream, adds 1 to it, and writes the result to the output stream. The TCP client connects to the server on port 5000, writes the number 3, receives the response from the server and prints it. The UDP server creates a datagram socket listening on port 4160, receives a packet from the client, prints the response data, and closes the socket. The UDP client creates a datagram socket, sends a "Hello world" string to the server on port 4160, and closes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

package MySarveur;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Serveur {

public static void main(String[] args) throws IOException{


ServerSocket ss = new ServerSocket(5000); // Crée un serveur écoutant
sur le port 5000
Socket s = new Socket();
System.out.println("En attente de la cnx ...");
s = ss.accept(); // Attente de la connexion du client
InputStream is = s.getInputStream();
OutputStream os = s.getOutputStream();
System.out.println("j att de lire le int");
int nb = is.read();
System.out.println("j ai recu le nbr "+nb);
int rslt = nb + 1 ;
System.out.println("j envoie le nbr "+rslt);
os.write(rslt);
s.close();

----------------------------------------------

package MyClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Client {

public static void main(String[] args) throws IOException{


Socket soc = new Socket("localhost",5000); // Connexion au serveur
InputStream is = soc.getInputStream();
OutputStream os = soc.getOutputStream();
os.write(3);
int r = is.read();
System.out.println("la rpns "+r);
soc.close();

=====================================================
package MyServeurUDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPServeur {

public static void main(String[] args) throws IOException{


DatagramSocket server = new DatagramSocket(4160); // Crée un socket UDP
écoutant sur le port 4160
byte[] receivData = new byte[256];
DatagramPacket packet = new
DatagramPacket(receivData,receivData.length);
server.receive(packet); // Attend de recevoir un paquet du client
String reponse = new String(packet.getData());
System.out.println("Reponse Data : "+reponse);
server.close();
}

----------------------------------------------------------
package MyClientUDP;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPClient {

public static void main(String[] args) throws IOException{


DatagramSocket client = new DatagramSocket();
InetAddress servadd = InetAddress.getByName("localhost"); // Adresse du
serveur (dans ce cas, localhost)
String str = "Hello world";
byte[] sendData = str.getBytes();
DatagramPacket p = new
DatagramPacket(sendData,sendData.length,servadd,4160);
client.send(p); // Envoi du message au serveur
client.close();
}

You might also like