[ back to toc ]

C

Date: 2002/03/05 11:35

Q:
A:
The name "c:/fu.txt" is not a valid file name under UNIX. This is the
reason. You can not make a file with that name. Use a different file name
that does not contain any "drive letter", which is typically DOS like file
name. For example you can name the file "fu.txt" that is going to be a
file in the actual directory.

There should not be other problem. Linux opens the files (from C) the same
way as DOS if you use fopen.

Regards,
Peter
==========================================================================
=
respected sir,

c server:

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>

#define BUF_SIZE 120
#define FILE_DATA "server.c"
#define SERVER_PORT 15000
#define FAIL -1

main()
{
int create_socket,new_socket,addrlen;
struct sockaddr_in address;
char temp[ BUF_SIZE ];
FILE *fpData;
fpData = fopen( FILE_DATA, "r" );

if( fpData == NULL )
printf("[%s] Access Denied File Cannot be Opened\n", FILE_DATA );
else
printf ( "[%s] file is exist\n", FILE_DATA );

if ( ( create_socket = socket( AF_INET,SOCK_STREAM,0 ) ) > 0 )
printf("The socket was created\n");

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( SERVER_PORT );

if (bind(create_socket,(struct sockaddr *)&address,sizeof(address)) ==
0)
printf("Binding Socket\n");

if ( listen( create_socket, 3 ) == FAIL )
printf ( "Socket Listen Fail\n" );

else

printf ( "Socket Listen Success\n" );

addrlen = sizeof(struct sockaddr_in);
new_socket = accept(create_socket,(struct sockaddr
*)&address,&addrlen);

if ( new_socket == FAIL )
printf ( "Socket accept Fail\n" );
else
printf("The Client %s is
connected...\n",inet_ntoa(address.sin_addr));

while( 1 )
{
fgets( temp, BUF_SIZE, fpData );
if ( feof ( fpData ) ) break;
send( new_socket, temp, BUF_SIZE, 0 );

}

fclose( fpData );
close(new_socket);
close(create_socket);
}
================================================
java client:

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

public class tcpClient1 {

public static void main(String[] args) {

int port = 15000;
String server = "192.168.4.22";
Socket socket = null;
String lineToBeSent;
String lineToBeRcd;
BufferedReader inFromServer;
int ERROR = 1;

// connect to server

try {
socket = new Socket(server, port);
System.out.println("Connected with server " +
socket.getInetAddress() +
":" + socket.getPort());
}

catch (UnknownHostException e) {
System.out.println(e);
System.exit(ERROR);
}
catch (IOException e) {
System.out.println(e);
System.exit(ERROR);
}

try {

inFromServer = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

while(true) {

lineToBeRcd = inFromServer.readLine();
if (lineToBeRcd==null) break;
System.out.println(lineToBeRcd);

}

}
catch (IOException e) {
System.out.println(e);
}

try {
socket.close();
}
catch (IOException e) {
System.out.println(e);
}
}
}

my client is in java & server is in c.Both client & server getting
connected & text file data are passing from c server(linux) to java client
but junk value is coming along with the original text. junk value is
mainly due to the buffersize(BUF_SIZE)in c.

how to come off from that junk display & how to clear the buffer size or
buffer content each time after sending a line to the client.

original text file is:(funt.c)

hai *NAME-DELETED*
hai priya
hi priyam

output in java is:

hai sanakari
!@%$$%$^%%#^^&%^^&*&(*)(*)())(^!@##@%#^+++)EW(QR(#$*$#*%^*%*%&^%&^&*^&*
%$$############hai priya
#@$*@$%$^%)&)%^*&)&)(*(_*_(+|+_|*&%$!~@#%^&^_)+|+_)(*&^!~@!$@^%$&(_){_)
+||_++_)+hi priyam
$#(#_+%)$+^%%$#@!!!!!!!!!!!!!!!!!!!!(%^%&(^%&%$@!!!!!!!!!!!!%^$&
376#%$%$

- *NAME-DELETED*

A:
I recommend that you rather use fread (or even read) instead of fgets and
then you will know the actual size of the bytes read. You may also memset
the buffer to contain only zero characters before reading the string into
it, but that is not a really good solution.

Regards,
Peter

[ back to toc ]