tcp client server communication

tcp client server communication

i am not been able to find the error in the program server:

import java.io.*; import java.net.*;

public class Server { public static void main(String[] args) throws Exception { String clientSentence; String sentence;

    ServerSocket welcomeSocket = new ServerSocket(6787);
    Socket connectionSocket = welcomeSocket.accept();
    BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
    DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
    BufferedReader serverInput = new BufferedReader(new InputStreamReader(System.in));

    sentence = serverInput.readLine();
    outToClient.writeBytes(sentence + '\n');

    while((clientSentence = inFromClient.readLine()) != null) {

        if(clientSentence.equals("exit"))
            break;

        System.out.println("FROM CLIENT:  " + clientSentence);

        if (serverInput != null) {
            sentence = serverInput.readLine();
            outToClient.writeBytes(sentence + '\n');
            if (sentence.equals("exit"))
                break;
        }
    }

    welcomeSocket.close();
    connectionSocket.close();
    inFromClient.close();
    outToClient.close();
    serverInput.close();
}

}

client:

import java.io.*;

import java.net.*;

public class Client { public static void main(String[] args) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6787); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    while((modifiedSentence = inFromServer.readLine()) != null){
        if (modifiedSentence.equals("exit"))
            break;

        System.out.println("FROM SERVER:  " + modifiedSentence);

        sentence = inFromUser.readLine();

        if (inFromUser != null) {
            outToServer.writeBytes("Typed:"+sentence + '\n');
            if(sentence.equals("exit"))
                    break;
        }


    }

    clientSocket.close();
    inFromUser.close();
    outToServer.close();
    inFromServer.close();

}

}

View Answers









Related Tutorials/Questions & Answers:
tcp client server communication
tcp client server communication  i am not been able to find the error in the program server: import java.io.*; import java.net.*; public class Server { public static void main(String[] args) throws Exception
MultiClient-server tcp-ip with GUI in java
MultiClient-server tcp-ip with GUI in java  i wanna MultiClient-server tcp-ip with GUI in java
Advertisements
MultiClient-server tcp-ip with GUI in java
MultiClient-server tcp-ip with GUI in java  i wanna MultiClient-server tcp-ip with GUI in java
Inter Server Communication - Struts
Inter Server Communication   Hi In my project I am using Struts2.0 ,I have 2 server one is web-logic server and another is Tomcat server. In web-logic server , there is link to call tomcat server for call struts application
Failed Client-Socket Communication
Failed Client-Socket Communication  I hve written a server program & a client program. The server is supposed to echo watever is typed in the client. I hve to get 16 values to be echoed. I hve created a string and all
ModuleNotFoundError: No module named 'koheron-tcp-client'
ModuleNotFoundError: No module named 'koheron-tcp-client'  Hi, My... named 'koheron-tcp-client' How to remove the ModuleNotFoundError: No module named 'koheron-tcp-client' error? Thanks   Hi, In your
Version of com.guardtime>ksi-service-client-tcp dependency
List of Version of com.guardtime>ksi-service-client-tcp dependency
Maven Dependency ksi-service-client-tcp >> 4.0.15
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.0.15 in your project
Maven Dependency ksi-service-client-tcp >> 4.2.32
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.2.32 in your project
Maven Dependency ksi-service-client-tcp >> 4.4.67
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.4.67 in your project
Maven Dependency ksi-service-client-tcp >> 4.6.80
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.6.80 in your project
Maven Dependency ksi-service-client-tcp >> 4.8.91
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.8.91 in your project
Maven Dependency ksi-service-client-tcp >> 4.0.16
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.0.16 in your project
Maven Dependency ksi-service-client-tcp >> 4.1.18
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.1.18 in your project
Maven Dependency ksi-service-client-tcp >> 4.3.54
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.3.54 in your project
Maven Dependency ksi-service-client-tcp >> 4.3.55
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.3.55 in your project
Maven Dependency ksi-service-client-tcp >> 4.5.71
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.5.71 in your project
Maven Dependency ksi-service-client-tcp >> 4.5.72
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.5.72 in your project
Maven Dependency ksi-service-client-tcp >> 4.7.85
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.7.85 in your project
Maven Dependency ksi-service-client-tcp >> 4.8.89
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.8.89 in your project
Maven Dependency ksi-service-client-tcp >> 4.9.95
You should include the dependency code given in this page to add Maven Dependency of com.guardtime >> ksi-service-client-tcp version4.9.95 in your project
Maven Repository/Dependency: com.guardtime | ksi-service-client-tcp
Maven Repository/Dependency of Group ID com.guardtime and Artifact ID ksi-service-client-tcp. Latest version of com.guardtime:ksi-service-client-tcp dependencies. # Version Release Date
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for ksi-service-client-tcp version 4.17.213
-client-tcp version 4.17.213. You can add these depency in your project to get com.guardtime:ksi-service-client-tcp:4.17.213 Java library in your project...-service-client-tcp version 4.17.213 In this section have given the dependency
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for ksi-service-client-tcp version 4.16.205
-client-tcp version 4.16.205. You can add these depency in your project to get com.guardtime:ksi-service-client-tcp:4.16.205 Java library in your project...-service-client-tcp version 4.16.205 In this section have given the dependency
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for ksi-service-client-tcp version 4.3.55
-client-tcp version 4.3.55. You can add these depency in your project to get com.guardtime:ksi-service-client-tcp:4.3.55 Java library in your project.. Maven...-service-client-tcp version 4.3.55 In this section have given the dependency code
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for ksi-service-client-tcp version 4.3.54
-client-tcp version 4.3.54. You can add these depency in your project to get com.guardtime:ksi-service-client-tcp:4.3.54 Java library in your project.. Maven...-service-client-tcp version 4.3.54 In this section have given the dependency code
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for ksi-service-client-tcp version 4.2.32
-client-tcp version 4.2.32. You can add these depency in your project to get com.guardtime:ksi-service-client-tcp:4.2.32 Java library in your project.. Maven...-service-client-tcp version 4.2.32 In this section have given the dependency code
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for ksi-service-client-tcp version 4.1.18
-client-tcp version 4.1.18. You can add these depency in your project to get com.guardtime:ksi-service-client-tcp:4.1.18 Java library in your project.. Maven...-service-client-tcp version 4.1.18 In this section have given the dependency code
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for ksi-service-client-tcp version 4.0.16
-client-tcp version 4.0.16. You can add these depency in your project to get com.guardtime:ksi-service-client-tcp:4.0.16 Java library in your project.. Maven...-service-client-tcp version 4.0.16 In this section have given the dependency code
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for ksi-service-client-tcp version 4.0.15
-client-tcp version 4.0.15. You can add these depency in your project to get com.guardtime:ksi-service-client-tcp:4.0.15 Java library in your project.. Maven...-service-client-tcp version 4.0.15 In this section have given the dependency code
Maven dependency for com.guardtime - ksi-service-client-tcp version 4.17.213 is released. Learn to use ksi-service-client-tcp version 4.17.213 in Maven based Java projects
-service-client-tcp released The developers of   com.guardtime - ksi-service-client-tcp project have released the latest version of this library on 28 Apr 2021, the released version of  com.guardtime - ksi-service-client-tcp
Maven dependency for com.guardtime - ksi-service-client-tcp version 4.18.217 is released. Learn to use ksi-service-client-tcp version 4.18.217 in Maven based Java projects
-service-client-tcp released The developers of   com.guardtime - ksi-service-client-tcp project have released the latest version of this library on 11 Jan 2022, the released version of  com.guardtime - ksi-service-client-tcp
Maven dependency for com.guardtime - ksi-service-client-tcp version 4.0.15 is released. Learn to use ksi-service-client-tcp version 4.0.15 in Maven based Java projects
-client-tcp released The developers of   com.guardtime - ksi-service-client-tcp project have released the latest version of this library on 29 Sep 2015, the released version of  com.guardtime - ksi-service-client-tcp
Maven dependency for com.guardtime - ksi-service-client-tcp version 4.16.205 is released. Learn to use ksi-service-client-tcp version 4.16.205 in Maven based Java projects
-service-client-tcp released The developers of   com.guardtime - ksi-service-client-tcp project have released the latest version of this library on 02 Jul 2020, the released version of  com.guardtime - ksi-service-client-tcp
Difference between Web Server and Application Server
the Application server allows business logic to client through various protocols like HTTP, TCP-IP etc. Web Server generates a response generally in HTML... HTML pages only while Application server provides methods that client
TCP socket
TCP socket  Respected sir/madam, Sir, i want to know about tcp sockets and udp datagrams which is used by mobile applications,shall i know how the chatting(sms) application can run by both people?how they enabled
TCP socket
TCP socket  Respected sir/madam, Sir, i want to know about tcp sockets and udp datagrams which is used by mobile applications,shall i know how the chatting(sms) application can run by both people?how they enabled
Communication Tube
Communication Tube       Communication Tube - web messenger chat client for the popular IM (instant messaging) networks: ICQ , MSN Messenger, Google Talk (GTalk) , IRC Read full
Java UDP
; TCP and UDP are transport protocols used for communication between computers. UDP... be used to send datagrams, using Datagram Sockets, to one another i.e. from client to server and server to client. A datagram is an independent, self-contained
Communication Effectiveness in Outsourcing, Communication Techiques in Outsourcing
applications, product development, client-server applications, server-based...Communication Effectiveness in Outsourcing  Introduction A good part of the communication in outsourcing relationships is achieved
Communication Portal
Communication Portal       A communication interface desinged with Web 2.0 technology, mission is to help peolpe communicate easily over a comfortable web interface Read full
server client comms
server client comms  i am trying to return frame through server client comms anyone can help me about that i want to return a class to client side but my class defined in server side
How TCP/IP works ?
How TCP/IP works ?  hello,, Can some body tell me how TCP/IP works?   hii,ADS_TO_REPLACE_1 TCP/IP is just a protocol that allows different nodes on the network/Internet to share resources and without TRANSMISSION
Client Server Java app
Client Server Java app  I developed a client server based java networking Instant Messaging app. The client program is needed to be run on the client computer whereas the server program is on server computer. This works in my
Client-Server Architecture
Client-Server Architecture       Client-server architecture can be considered...; and a client machine where server has some resources that can be shared by different
Interthread Communication
Inter-Thread Communication   ... communication among threads which reduces the CPU?s idle time i.e. A process where...; This technique is known as Interthread communication which is implemented by some
ModuleNotFoundError: No module named 'oshino-tcp'
ModuleNotFoundError: No module named 'oshino-tcp'  Hi, My Python... 'oshino-tcp' How to remove the ModuleNotFoundError: No module named 'oshino-tcp' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'simple-tcp'
ModuleNotFoundError: No module named 'simple-tcp'  Hi, My Python... 'simple-tcp' How to remove the ModuleNotFoundError: No module named 'simple-tcp' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'tcp_blackhole'
ModuleNotFoundError: No module named 'tcp_blackhole'  Hi, My... 'tcp_blackhole' How to remove the ModuleNotFoundError: No module named 'tcp_blackhole' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'tcp_chat'
ModuleNotFoundError: No module named 'tcp_chat'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'tcp_chat' How to remove the ModuleNotFoundError: No module named 'tcp_chat

Ads