Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners tcp client server communication
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


baanku
tcp client server communication
0 Answer(s)      4 years and 2 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();

}

}


Advertisement
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
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
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
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
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
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
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
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
how to connect client to server using Sockets
how to connect client to server using Sockets  how to connect client to server using Sockets
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
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
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
Create text file at client's directory from server.
Create text file at client's directory from server.  Need java code to create text file at client's directory from server..... Please Help
Server side validation vs client side validation
Server side validation vs client side validation  Can any one tell me... of Server side validation vs client side validation.   The client side validation runs in the browser where as server side validation runs on server
File transfer from client to server - Java Beginners
File transfer from client to server  hi,, I've been trying to make an application where the user select a file using JFileChooser and then the program directly send it to the server using client/server sockets, I've tried
Multicast under UDP(client server application)
Multicast under UDP(client server application)   UDP is used...: In this project, you will write two classes RMulticastServer (called server in the following) and RMulticastClient (called client in the following) in Java
RMI Client And RMI Server Implementation
RMI Client And RMI Server Implementation  ... comprises of the two separate programs, a server and a client. A typical server... application provides the mechanism by which the server and the client communicate
Client Side and Server Side coding
Client Side and Server Side coding       The combo of client side and server side coding... to write code in both client side and server side. Some other combinations are &ndash
What do you think about client-side/server-side coding ?
What do you think about client-side/server-side coding ?  Hi, What do you think about client-side/server-side coding
Hi i want java code that diaplays server scren at the client side
Hi i want java code that diaplays server scren at the client side  Hi I'm Shivaprasad i want java socket code that shows the server side screen when client gets connected with the server. Please help me   Please visit
File download from server to client machine - JSP-Servlet
File download from server to client machine  hi, I want to save a pdf file to my client machine from a button provoded in a jsp page.The file... the generated report in server as follow. exporter.exportIntoPdfFile("c://reports
How TCP/IP works ?
How TCP/IP works ?  hello,, Can some body tell me how TCP/IP works?   hii, TCP/IP is just a protocol that allows different nodes on the network/Internet to share resources and without TRANSMISSION CONTROL PROTOCOL
how to use a JInternalFrame in a JFrame to create a chat window in a client/server app?
how to use a JInternalFrame in a JFrame to create a chat window in a client/server app?  how to use a JInternalFrame within a JFrame? i want to create a client/server app. i know that JIternalFrame should be used
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 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
Communication Effectiveness in Outsourcing, Communication Techiques in Outsourcing
of the project- whether it is software applications, product development, client-server...Communication Effectiveness in Outsourcing  Introduction A good part of the communication in outsourcing relationships is achieved
message sending and receiving using UDP TCP in J2ME
message sending and receiving using UDP TCP in J2ME   I need the simple program for message sending and receiving using UDP TCP in J2ME. Could u pls
Datagram in network environment
;    In a network environment the client and the server communicate with each-other by reliable channel like TCP socket which have dedicated point-to-point channel between client and server. All data sent over
 

 

 

DMCA.com