PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Betr: sw-probleme mit ethernet-verbindung server & client



nomad
06-12-2004, 13:21
Betr: sw-probleme mit ethernet-verbindung server & client
no2. hi leute
problem geloest,
im client-programm:
ergaenzung:
chaf buffer{128];
und:
while(cnt <= 5)
{
cnt++;
write(sockfd, &sendChar, 1);

read(sockfd, &buffer,sizeof(buffer));
printf("ServerInput[%d]: %s\n",cnt,buffer);

}
alles OK
mfg nomad
----original message ------------------------------------------------------------------------------------
hi leute,
moechte eine eth-verbindung zwischen einen linuxLaptop und einem linuxDesktopRechner aufbauen.
laptop = server
desktop = client

hab einiges im buch "Linux Programmierung von alan cox" gefunden.
hab das problem das sehrwohl eine verbindung aufgebaut wird,
die sachen kommen auch an, aber leider verstuemmelt. ss. output:
folgende pgm sind beteiligt:
-------------------------------------------------------------------------------------------------------
laptop: server41.c
-----------------------
compile: cc -o server41 server41.c
run : ./server41 &
/************************************************** ************************************************/
sendDataStrings
- no 1: $GPRMC:0:A:101139.00,A:8.698889:47.521667:0.102880 :351.100006:
- no 2: $NNAUT:326:330:8.698889:47.521667:
- no 3: $NSTART_NAUT:326:330:8.698889:47.521667:
- no 4: $MINMAXNAUT:47.521445:47.522336:8.699665:8.698083:
- no 5: $WPOINTXY:326:330:255:175:386:175:
- no 6:$WPOINTNAUT:8.698889:47.521667:8.698713:47.52198 2:8.699037:47.521982:
************************************************** ***********************************************
reference: This program, server4.c, begins in similar vein to our last server,
with the notable addition of an include for the signal.h header file.
The variables and the procedure of creating and naming a socket are the same.

************************************************** ***********************************************/

#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <sys/signal.h>
#include <unistd.h>
#include <math.h>

int len, result;
char ch;
char recChar;

// sendDatasStrings
// - no 1:
char sendData_1[] = "$GPRMC:0:A:101139.00,A:8.698889:47.521667:0.102880 :351.100006:";
// -no 2:
char *sendData_2 = "$NNAUT:326:330:8.698889:47.521667:";
// - no 3:
char *sendData_3 = "$NSTART_NAUT:326:330:8.698889:47.521667:";
// - no 4:
char *sendData_4 = "$MINMAXNAUT:47.521445:47.522336:8.699665:8.698083:";
// - no 5:
char *sendData_5 = "$WPOINTXY:326:330:255:175:386:175:";
// - no 6:
char *sendData_6 = "$WPOINTNAUT:8.698889:47.521667:8.698713:47.521982: 8.699037:47.521982:";

char buffer[128];
int serverCnt;

// network
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;

/************************************************** ***********************************************/

void gotoxy(int x, int y) { printf("\033[%d;%dH", y, x); fflush(stdout); }
void clrscr(void) { printf("\033[H\033[J"); fflush(stdout); }

/************************************************** ***********************************************/

int main()
{
clrscr();
gotoxy(2,1);
printf("The Ethernet-Server Version 0.0.4.1.");

serverCnt = 0;

server_sockfd = socket(AF_INET, SOCK_STREAM, 0);

server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(9734);
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len);

// Create a connection queue, ignore child exit details and wait for clients.

listen(server_sockfd, 5);

signal(SIGCHLD, SIG_IGN);

while(1)
{
gotoxy(2,2);
printf("server waiting\n");

// Accept connection.

client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address, &client_len);

// Fork to create a process for this client and perform a test to see
// whether we're the parent or the child.

if(fork() == 0)
{
// If we're the child, we can now read/write to the client on client_sockfd.
// The five second delay is just for this demonstration.

read(client_sockfd, &recChar, 1);
gotoxy(2,3);
printf("Input: %c\n",recChar);

for(serverCnt = 0;serverCnt <= 5;serverCnt++)
{
if(serverCnt == 0)
{
strcpy(buffer,sendData_1);
write(client_sockfd, &buffer,sizeof(buffer));
printf("Cnt 0\n");
}
if(serverCnt == 1)
{
strcpy(buffer,sendData_2);
write(client_sockfd, &buffer,sizeof(buffer));
printf("Cnt 1\n");
}
if(serverCnt == 2)
{
strcpy(buffer,sendData_3);
write(client_sockfd, &buffer,sizeof(buffer));
printf("Cnt 2\n");
}
if(serverCnt == 3)
{
strcpy(buffer,sendData_4);
write(client_sockfd, &buffer,sizeof(buffer));
printf("Cnt 3\n");
}
if(serverCnt == 4)
{
strcpy(buffer,sendData_5);
write(client_sockfd, &buffer,sizeof(buffer));
printf("Cnt 4\n");
}
if(serverCnt == 5)
{
strcpy(buffer,sendData_6);
write(client_sockfd, &buffer,sizeof(buffer));
printf("Cnt 5\n");
}

} // end for-loop

close(client_sockfd);
exit(0);
}

// Otherwise, we must be the parent and our work for this client is finished.

else
{
close(client_sockfd);
}
} // end while
}

/************************************************** ***********************************************/
desktop: client35.c
cc -o client35 client35.c
run : ./client35 &
************************************************** ***********************************************/

#include <sys/types.h>

#include <sys/socket.h>

#include <stdio.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <unistd.h>

int sockfd;

int len;
int i;

struct sockaddr_in address;

int result;
char sendChar = '1';

char ch;
int cnt;
/************************************************** ***********************************************/
// from conio.h
void gotoxy(int x, int y) { printf("\033[%d;%dH", y, x); fflush(stdout); }
void clrscr(void) { printf("\033[H\033[J"); fflush(stdout); }


int main()

{

// Create a socket for the client.



sockfd = socket(AF_INET, SOCK_STREAM, 0);



// Name the socket, as agreed with the server.



address.sin_family = AF_INET;

address.sin_addr.s_addr = inet_addr("192.168.100.1");

address.sin_port = htons(9734);

len = sizeof(address);



// Now connect our socket to the server's socket.



result = connect(sockfd, (struct sockaddr *)&address, len);



if(result == -1)
{

perror("oops: client35");

exit(1);

}
clrscr();
printf("\n");
gotoxy(2,1);
printf("The Ethernet-Client Version 0.0.3.5.");



// We can now read/write via sockfd.

cnt = 0;

while(cnt != 6)
{
cnt++;
write(sockfd, &sendChar, 1);
printf("Char from server = "); // %c",ch);

for(i=0;i<=70;i++)
{

read(sockfd, &ch, 1);
printf("%c",ch);
}
printf("\n");
}
close(sockfd);
printf("Done\n");
exit(0);

}
/************************************************** ***********************************************/
Output:
The Ethernet-Client Version 0.0.3.5.
Char from server = $GPRMC:0:A:101139.00,A:8.698889:47.521667:0.102880 :351.100006:
Char from server = $NNAUT:326:330
Char from server = :8.698889:47.521667:521667:0.102880:351.100006:
Char from server = $NSTART_NAUT:326:330:8.69888
Char from server = 9:47.521667::0.102880:351.100006:
Char from server = $MINMAXNAUT:47.521445:47.522336:8.699665:8
Done
/************************************************** ***********************************************/

da ich noch relativ wenig erfahrung mit dieser art von pgm,s habe waere ich froh
um jeden tip oder hinweis, was ich besser machen koennte
mfg nomad