- Client code:-
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <netdb.h>
-
#include <stdio.h>
-
#include <sys/unistd.h>
-
#include <unistd.h>
-
#include <stdlib.h>
-
#include <strings.h>
-
-
-
-
#define STRLEN 1024
-
-
int main(int argc, char *argv[]) {
-
-
int sockfd, n, slen;
-
-
char sendline[STRLEN], recvline[STRLEN];
-
struct sockaddr_in servaddr;
-
int gethostname(char *name, size_t len);
-
struct hostent *h;
-
char hostname[128];
-
struct sockaddr_in sa;
-
-
if(argc != 3)
-
{
-
printf("Usage: %s <Enter Server Address> <Enter port no >\n", argv[0]);
-
exit(0);
-
}
-
-
-
gethostname(hostname, sizeof hostname);
-
printf("\n\nYou are connected from:- %s\n", hostname);
-
-
/* create hostent structure from user entered host name*/
-
if ( (h = gethostbyname(argv[1])) == NULL)
-
{
-
printf("\n%s: error in gethostbyname()", argv[0]);
-
exit(0);
-
}
-
-
-
/* create server address structure */
-
bzero(&servaddr, sizeof(servaddr)); /* initialize it */
-
servaddr.sin_family = AF_INET;
-
servaddr.sin_addr.s_addr = htonl(inet_addr(argv[1]));
-
servaddr.sin_port = htons(atoi(argv[2])); /* get the port number from argv[2]*/
-
-
if ( (sockfd = socket(AF_INET,SOCK_DGRAM, 0)) < 0)
-
-
{
-
printf("\n%s: error in socket()", argv[0]);
-
exit(0);
-
}
-
-
-
-
printf("\nEnter The name of the file you want to search:- ");
-
scanf("%s",sendline);
-
-
if (sendto(sockfd, sendline, sizeof(sendline), 0, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
-
{
-
printf("\n%s: error in sendto()", argv[0]);
-
exit(0);
-
-
}
-
-
slen = sizeof(servaddr);
-
if ( (n = recvfrom(sockfd, recvline, STRLEN, 0, (struct sockaddr *) &servaddr, &slen)) < 0) {
-
printf("\n%s: error in recvfrom()", argv[0]);
-
exit(0);
-
}
-
-
recvline[n] = 0;
-
-
-
fputs(recvline, stdout);
-
-
close(sockfd);
-
-
}
-
-
-
-
Server Code:-
-
-
#include <sys/types.h>
-
#include <sys/socket.h>
-
#include <netinet/in.h>
-
#include <arpa/inet.h>
-
#include <netdb.h>
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <strings.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
-
#define STRLEN 1024
-
-
int main(int argc, char *argv[]) {
-
-
int sockfd, clen, n;
-
struct sockaddr_in servaddr, cliaddr;
-
struct stat buffer;
-
int status;
-
char a[50];
-
struct stat m;
-
-
-
char str_in[STRLEN];
-
-
if (argc != 2) {
-
printf("\nUsage: %s < Enter port no>", argv[0]);
-
exit(0);
-
}
-
-
-
/* socket creation */
-
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
-
printf("\n%s: error in socket()", argv[0]);
-
exit(0);
-
}
-
-
/* create local server socket structure */
-
bzero(&servaddr, sizeof(servaddr));
-
servaddr.sin_family = AF_INET;
-
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
-
servaddr.sin_port = htons(atoi(argv[1]));
-
-
/* bind it to an address */
-
if (bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))< 0)
-
{
-
printf("\n%s: error in bind()", argv[0]);
-
exit(0);
-
}
-
-
clen = sizeof(cliaddr);
-
if ( (n = recvfrom(sockfd, str_in, STRLEN, 0, (struct sockaddr *) &cliaddr, &clen)) < 0) {
-
printf("\n%s: error in recvfrom()", argv[0]);
-
exit(0);
-
}
-
printf("%s\n",str_in);
-
-
sprintf (a,"data/%s", str_in);
-
printf("%s\n",a);
-
status= stat( a, &buffer );
-
-
if (status ==0)
-
{
-
printf("File found");
-
sprintf(str_in,"%s",buffer.st_size);
-
}
-
else
-
{
-
printf("File not found");
-
sprintf(str_in,"0");
-
}
-
-
if (sendto(sockfd,str_in,n, 0, (struct sockaddr *) &cliaddr, clen) < 0) {
-
printf("\n%s: error in sendto()", argv[0]);
-
exit(0);
-
}
-
-
-
-
-
-
close(sockfd);
-
-
}
-
-
1 1685
Have you stepped through this using your debugger?
It's what I would have to do.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Thomas |
last post by:
Hallo.
Habe auf einem SeSE 9.1 Server due neue MySQL 4.1.11 und den
dazugehörigen client installiert.
#: mysql -V
mysql Ver 14.7 Distrib 4.1.11, for pc-linux-gnu (i686)
Soweit so gut der...
|
by: Zvi Danovich |
last post by:
Hi,
I am a newby in ASP.NET, and till now wrote only simple "classic" WEB-sites.
But - the time came, and now I need that:
1. Server will "listen" for some events on its local machine
2....
|
by: Lau Lei Cheong |
last post by:
I have an aspx page which have the following code segment:
<script>
// I do something here
template = "<input id=\"hidden_info_1\" type=\"hidden\"
runat=\"server\">
// I do other things here...
|
by: vidya |
last post by:
Hi,
I have a button which is a web control. I have some validation in javascript
for the button in .aspx file and some in the button onclick event in code
behind(C#). I need to get through both...
|
by: jacob |
last post by:
Hi everyone:
I have a problem in ASP NET. Here is my situation.
I developed an ASP NET application. It concerns data statistics.
DB is oracle 10g, Web server is IIS 6.0, Server OS is Win2003,...
|
by: drk.kumar |
last post by:
I have an implementation issue with WMI scripts to check the user
machine processor. The implementation is working fine in the local
machine (Windows XP operating system). It is throwing script...
|
by: Wimpie van Lingen |
last post by:
Hey
I have some more questions with regards to Remoting in .NET 2. I'm using TCP with the Binary formatter.
My solution consists of 4 projects:
- Class Library containing the server classes...
|
by: Vili |
last post by:
Hi all
I am having a problem here
Is it possible to fire a server side function from client side?
ie. I have a function on codebehind
Sub DoSomething(o as object)
|
by: haytham2008 |
last post by:
can i reach file using any client script ??
eg. javascript or any other language and i have access to read file without the user allow me
please i need to know about this thing :$
|
by: Simon |
last post by:
I heard that we could do that by using AJAX. Could anybody share how to do
it? Thanks.
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |