15,615,668 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View Python questions
View Javascript questions
View C++ questions
View Java questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by saranyaayyappan (Top 8 by date)
saranyaayyappan
9-Jul-15 3:53am
View
Please tell which part of my code has bug(Server or Client).It will helped me to which part of the code will i need to change
saranyaayyappan
1-Jul-15 1:06am
View
Please give me a solution...
saranyaayyappan
1-Jul-15 1:05am
View
Hai
Please check the below code and let me know your suggestions.Here i am only able to perform one way communication..not get the same file as response from server..
Thanks in advance
Server code:
Hide Expand Copy Code
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
private static Socket socket;
static int current=0;
public final static String FILE_TO_RECEIVED = "/home/sosdt010/Desktop/Testfile/text1.txt";
public final static int FILE_SIZE = 4939993 ;
public static void main(String[] args)
{
try
{
int port = 6777;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 6777");
//Server is running always. This is done using this while(true) loop
while(true)
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos=new FileOutputStream(FILE_TO_RECEIVED );
BufferedOutputStream bos=new BufferedOutputStream(fos);
byte [] mybytearray = new byte [FILE_SIZE];
int bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead > -1);
bos.write(mybytearray, 0 , current);
//bos.close();
System.out.println("Message received from client is "+current);
//Sending the response back to the client.
OutputStream out = socket.getOutputStream();
File myFile=new File( FILE_TO_RECEIVED);
FileInputStream fis=new FileInputStream(myFile);
byte [] mybytearray1 = new byte [(int)myFile.length()];
int len ;
while ((len = fis.read(mybytearray1)) >= 0) {
out.write(len);
System.out.println("Message sent to the client is "+ current);
out.flush();
//fis.close();
}
}}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
}
Client Code:
Hide Expand Copy Code
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
public class Client
{
public final static String FILE_TO_SEND = "/home/sosdt010/Desktop/doc/character.txt";
private static Socket socket;
public final static String FILE_TO_RECEIVED = "/home/sosdt010/Desktop/Testfile/text2.txt";
public final static int FILE_SIZE = 4939993 ;
static int current=0;
public static void main(String args[])
{
try
{
String host = "10.170.0.38";
int port = 6777;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the message to the server
OutputStream out = socket.getOutputStream();
saranyaayyappan
30-Jun-15 9:06am
View
I am performing simple bidirectional client server communication.Here i am open the socket and send text file from client to server.this was performed well but when i am send back the same file as response from server to client at the same port..not working
saranyaayyappan
26-Jun-15 3:28am
View
I have tried so many links regarding bi-directional communication..Nothing was working please share me example code for bidirectional communication
saranyaayyappan
25-Jun-15 8:40am
View
The Problem is server receives the file but client doesn't receive response file...i want bi directional communication but its works only single way
saranyaayyappan
25-Jun-15 8:01am
View
I have already tried with a word file, but i am not able to send and receive data bi-directionally. Could you share me some link regarding this. I have tried many ref from google, but nothing has been sorted.
saranyaayyappan
24-Jun-15 3:41am
View
Tried all actions but i cant get the output.Can i post the code for better understanding
Show More