Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

tcp client server communication

Ads

 
 

Share on Google+Share on Google+

baanku
tcp client server communication
0 Answer(s)      6 years and 9 months ago
Posted in : Java Beginners

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();

}

}

Ads
View Answers
Ads









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
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
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
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
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
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.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.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.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.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.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 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.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.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.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.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.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.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.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.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
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
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... Application server provides methods that client applications can call. Application
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
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
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
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
Server calling of .exe file in the client
Server calling of .exe file in the client   I have a requirement with java webapplication. We are using jboss as appserver on Linux machine. How can I call a .exe file from client machine? I want to run .exe from webpage like
proxy server and client using java - Java Beginners
proxy server and client using java  how to write program in java for proxy server and client
how to connect client to server using Sockets
how to connect client to server using Sockets  how to connect client to server using Sockets