Java HTTP Server and Client

Create chat application using either TCP protocol

Making simple chat application using "Socket" Programming. Here is the code for TCP/IP Server and Client. 

GTU; Practical 1: Advance  Java : Create chat application using either TCP  protocol

 

Code of Server

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chat_server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
*
* @author Administrator
*/
public class Chat_Server {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.print("Server is UP");

try{

ServerSocket welcome = new ServerSocket(1234);
Socket skt= welcome.accept();

//Send information to client for connection

PrintStream ps = new PrintStream(skt.getOutputStream());
ps.print("Server is connectedn");

//Read Line From Client

BufferedReader br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
String msg = br.readLine();
System.out.println("Client: " + msg);
}catch(Exception e){
System.err.println(e.toString());
}
}

}

Code of Client

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package chat_client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
*
* @author Administrator
*/
public class Chat_Client {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {

Socket skt = new Socket("localhost", 1234);
BufferedReader br = new BufferedReader(new InputStreamReader(skt.getInputStream()));
String str = br.readLine();
System.out.println("Server:" + str);

PrintStream ps = new PrintStream(skt.getOutputStream());
ps.printf("Hello Server this is Clientn");

} catch (Exception e) {
System.err.println(e.toString());
}
}

}

Share Post