witam pisze prosty komunikator mam problem programem clienta, kod servera
/***************************************************************************
* Copyright (C) 2010 by root *
* [email protected] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
int main(int argc, char* argv[]){
int fd;
if ((fd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("Socket fail");
exit(0);
}
struct sockaddr_in s;
s.sin_family = AF_INET;
s.sin_addr.s_addr=INADDR_ANY;
s.sin_port = htons(atoi(argv[1]));
int val=1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,&val,sizeof(val));
int bind_er=bind(fd,(struct sockaddr *)&s,sizeof(s));
if (bind_er==-1){
perror("bind error");
exit(0);
}
listen(fd,5);
for (;;) {
struct sockaddr_in client;
int len=sizeof(client);
int cfd;
printf("czekam na klienta");
cfd=accept(fd,(struct sockaddr *)&client,&len);
if (cfd == -1) {
perror("accept");
exit(0);
}
write(cfd,"hello!n",7);
close(cfd);
}
}
cod clienta
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
int main(int argc, char* argv[]){
char message[100];
int _socket;
if ((_socket = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
perror("Socket fail");
exit(0);
}
struct sockaddr_in s;
s.sin_family = AF_INET;
inet_aton(argv[0s],&s.sin_addr);
s.sin_port = htons(atoi(argv[2]));
int _connect;
if ((_connect = connect(_socket,(struct sockaddr *)&s,sizeof(s))) == 1)
{
perror("Connect fail");
exit(0);
}
int _read;
if ((_read = read(_socket,&message,sizeof(message))) == -1)
{
perror("Read error");
exit(0);
}
else
{
message[_read]='\0';
printf("%s",message);
}
if (close(_socket)== -1){
perror("Close error");
exit(0);
}
return 0;
}
zakazdym razem przy kliencie dostaje read error:/
no i server jakby wisie niby sie kompilje przy debugowaniu tez niby jest oki ale nie dziala poprawnie