i am trying to wrire a word and change it to capital letters
using client-server. im not able to write any words when i run the
file in netbeans or command promot. is it something with my
code?
/*
* 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 assignment3retake;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
*
*/
public class Capitalizer implements Runnable{
private Socket socket;
Capitalizer(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
System.out.println("Connected: " + socket);
try {
// TODO Create socket reader
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
// TODO Create socket writer
PrintWriter out = new PrintWriter(socket.getOutputStream());
// Read from the socket and using a loop, change the case to upper and write to the socket
String sentence = in.readLine();
while(sentence != null){
sentence.toUpperCase();
System.out.println(sentence + in.readLine());
}
in.close();
out.close();
} catch (Exception e) {
System.out.println("Error:" + socket);
} finally {
try {
socket.close();
} catch (IOException e) {
}
System.out.println("Closed: " + socket);
}
}
}
package assignment3retake;
import java.net.*;
import java.io.*;
import java.io.IOException;
import java.net.Socket;
/**
*
*/
public class Client {
public static void main(String[] args){
try{
//connect to server
Socket sock = new Socket("127.0.0.1",59898);
PrintStream sout = new PrintStream(sock.getOutputStream());
// InputStream in = sock.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(sock.getInputStream()));
BufferedReader uin = new BufferedReader(new InputStreamReader(System.in));
String sentence;
while(true){
System.out.flush();
sentence = uin.readLine();
if(sentence == null)break;
sout.println(sentence);
sentence = bin.readLine();
if(sentence == null){
//close the socket
sock.close();
System.out.println("Connection closed");
break;
}
System.out.println(sentence);
}
}catch(IOException ioe){
System.err.println(ioe);
}
}
}
/*
* 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 assignment3retake;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
*/
public class Server {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
try (ServerSocket listener = new ServerSocket(59898)) {
System.out.println("The capitalization server is running...");
// TODO Create a thread pool of 10 clients
ExecutorService pool = Executors.newFixedThreadPool(10);
while (true) {
Socket s = listener.accept();
// TODO call pool.execute() with a parameter which is an instance of the Capitalizer class
pool.execute(new Capitalizer(s));
}
}
}
}
I have change your code. I have used DataInputStream and DataOutputStream to recieve and send data from client to server.
Code is 100% runnable.
Run Server.java at first and then client
-----------------------------------------------------------------------------------------------------------------------------------------
Capitalizer.java
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
*
*
*
*/
public class Capitalizer implements Runnable {
private Socket socket;
Capitalizer(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
System.out.println("Connected: " + socket);
try {
DataInputStream in = new DataInputStream(
new BufferedInputStream(socket.getInputStream()));
String sentence = in.readUTF();
// reads message from client until "Over" is sent
while (sentence!=null)
{
try
{
sentence=sentence.toUpperCase();
System.out.println(sentence);
sentence = in.readUTF();
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection");
// close connection
in.close();
} catch (Exception e) {
System.out.println("Error:" + socket);
} finally {
try {
socket.close();
} catch (IOException e) {
}
System.out.println("Closed: " + socket);
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
Client.java
import java.net.*;
import java.io.*;
import java.io.IOException;
import java.net.Socket;
/**
*
*
*
*/
public class Client {
public static void main(String[] args) {
try {
//connect to server
Socket sock = new Socket("127.0.0.1", 59898);
// takes input from terminal
DataInputStream input = new DataInputStream(System.in);
// sends output to the socket
DataOutputStream out = new
DataOutputStream(sock.getOutputStream());
String sentence;
while (true) {
System.out.flush();
sentence = input.readLine();
out.writeUTF(sentence);
if (sentence == null) {
//close the socket
sock.close();
System.out.println("Connection closed");
break;
}
System.out.println(sentence);
}
} catch (IOException ioe) {
System.err.println(ioe);
}
}
}
----------------------------------------------------------------------------------------------------------------------------------------------
Server.java
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
*
*/
public class Server {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
try (ServerSocket listener = new ServerSocket(59898)) {
System.out.println("The capitalization server is running...");
// TODO Create a thread pool of 10 clients
ExecutorService pool = Executors.newFixedThreadPool(10);
while (true) {
Socket s = listener.accept();
// TODO call pool.execute() with a parameter which is an instance of the Capitalizer class
pool.execute(new Capitalizer(s));
}
}
}
}
--------------------------------------------------------------------------OUTPUT---------------------------------------------------------------
Server Side
Client 1
Client 2
Get Answers For Free
Most questions answered within 1 hours.