I am trying to implement a client-server program.
However, while the server program runs fine, the client program exits with the error::
recv: transport endpoint not connected.
Its been puzzling me for quite a while now. Some help will be greatly appreciated!
Here is the server program ::
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
int main()
{
int sockid,newsockid,pid;
struct sockaddr_in myaddr,clientaddr;
socklen_t x;
x=sizeof(clientaddr);
char msg[20];
sockid=socket(AF_INET,SOCK_STREAM,0);
if(sockid == -1)
perror("SOCKET");
memset(&myaddr,0,sizeof(myaddr));
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(8888);
myaddr.sin_addr.s_addr=inet_addr(INADDR_ANY);
if(bind(sockid,(const struct sockaddr *)&myaddr,sizeof(myaddr)) == -1)
perror("bind");
listen(sockid,5);
newsockid=accept(sockid,(struct sockaddr *)&clientaddr,&x);
if(newsockid == -1)
perror("accept");
printf("NEW CLIENT ADDR:: %s",ntohs(clientaddr.sin_addr.s_addr));
pid=fork();
while(1)
{
if(pid == 0)
{
memset(msg,'\0',20);
if(recv(newsockid,msg,sizeof(msg),0) == -1)
perror("recv");
puts(msg);
if(strcmp(msg,"exit") == 0)
{
close(newsockid);
exit(getpid());
}
}
if(pid!=0)
{
memset(msg,'\0',20);
gets(msg);
if(send(newsockid,msg,sizeof(msg),0) == -1)
perror("send");
if(strcmp(msg,"exit") == 0)
{
close(newsockid);
exit(getpid());
}
}
}
return 0;
}
Also here is the client side of things::
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
int main()
{
int pid,sockfd;
char msg[20];
struct sockaddr_in myaddr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd == -1)
perror("socket");
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(8888);
myaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
if(connect(sockfd,(struct sockaddr *)&myaddr,sizeof(myaddr)) == -1)
perror("connect");
pid=fork();
while(1)
{
if(pid == 0)
{
memset(msg,'\0',20);
if(recv(sockfd,msg,sizeof(msg),0) == -1)
{
perror("recv");
exit(getpid());
}
puts(msg);
if(strcmp(msg,"exit") == 0)
{
close(sockfd);
exit(getpid());
}
}
if(pid!=0)
{
memset(msg,'\0',20);
gets(msg);
if(send(sockfd,msg,sizeof(msg),0) == -1)
perror("send");
if(strcmp(msg,"exit") == 0)
{
close(sockfd);
exit(getpid());
}
}
}
return 0;
}
There are 2 bugs in the server code.
One is in this line:
myaddr.sin_addr.s_addr=inet_addr(INADDR_ANY);
The inet_addr function expects a character string, while INADDR_ANY constant is an IP number with all zeros. The server code crashed with segmentation fault for me until this line was corrected (did it really work for you?). The correct usage:
myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
Also, printf line is not correct - address is not short, but long, also you can't use %s (it would most likely crash). Note, that the compiler (well, gcc at least) warns about it - you should always enable and read compiler warnings. As a quick fix I've changed it to:
printf("NEW CLIENT ADDR:: %08x",ntohl(clientaddr.sin_addr.s_addr));
This is fine as long as you can read IP in hex :)
With those fixes it worked fine for me (I mean both server and client) - note, that there can be other bugs, I have to admit I didn't read it too carefuly.
Related
I'm coding a client/server, the client simply sends a message to the server that he will print the message.
To do this I used sockets and localhost. Here is the code:
server:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include"thpool.h"
#include"functions.h"
#define N 7
#define SERVER_PATH "/tmp/server"
int main(void){
unlink(SERVER_PATH);
struct sockaddr_un stru;
int sock_serv, new_sock;
int opt = 1;
struct sockaddr* cliaddr;
char buff[N];
cliaddr = malloc(sizeof(struct sockaddr));
socklen_t addrlen = strlen((char* )cliaddr);
if((sock_serv = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
printf("socket creation error");
exit(-1);
}
bzero(&stru, sizeof(struct sockaddr_in));
stru.sun_family = AF_UNIX ;
strncpy (stru.sun_path, SERVER_PATH, sizeof(stru.sun_path));
if((bind(sock_serv, (struct sockaddr*) &stru , sizeof(struct sockaddr_un ))) < 0){
perror("bind failed");
exit(EXIT_FAILURE);
}
if(listen(sock_serv, SOMAXCONN) < 0){
perror("listen error\n");
exit(EXIT_FAILURE);
}
if((new_sock = accept(sock_serv, NULL, 0)) < 0){
perror("accept error\n");
exit(EXIT_FAILURE);
}
read(new_sock , buff, N) ;
printf("Server got: %s\n" , buff);
close(sock_serv);
close(new_sock);
unlink(SERVER_PATH);
return 0;
}
and here is the client:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include<errno.h>
#include"thpool.h"
#include"functions.h"
#define SERVER_PATH "/tmp/server"
int main(void){
int sock_cl;
struct sockaddr* sa;
socklen_t sa_lenght;
sa = malloc(sizeof(struct sockaddr));
sa_lenght = strlen((char* )sa);
if((sock_cl = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
perror("socket creation error");
exit(EXIT_FAILURE);
}
sa->sa_family = AF_UNIX ;
strncpy (sa->sa_data, SERVER_PATH, sizeof(sa->sa_data));
while (connect(sock_cl , (struct sockaddr*)&sa , (socklen_t)sa_lenght) == -1) {
perror("connection to the server failed");
exit(EXIT_FAILURE);
}
write (sock_cl, "Hello!", 7);
printf("message sended\n");
close(sock_cl);
return 0;
}
I have a problem with the connect() function, the error is "invalid argument". Note that I first executed the server and then the client, so is not that the problem.
This is how you define sa, as a pointer to struct sockaddr.
struct sockaddr* sa;
Here you take the address of the variable sa and cast it to the type of sa.
(struct sockaddr*)&sa
The result is a pointer to pointer to struct sockaddr, which gets brute force cast to pointer to struct sockaddr.
Type-casting is a trap and you got caught in it.
To solve, I recommend using a tutorial on sockets.
I think that when comparing your client and the example client in this tutorial especially the pointer level issue you have created becomes nicely visible:
https://www.cs.rpi.edu/~moorthy/Courses/os98/Pgms/socket.html
Your server and client are both misusing the sockaddr... structures. The error on the server side doesn't affect anything because it is not actually using the faulty sockaddr it allocates, it just leaks. But your client is completely misusing the sockaddr that is passed to connect(), which is why connect() fails.
Try this instead:
server
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include "thpool.h"
#include "functions.h"
#define N 7
#define SERVER_PATH "/tmp/server"
int main(void){
unlink(SERVER_PATH);
struct sockaddr_un stru;
int sock_serv, new_sock;
ssize_t bufflen;
char buff[N];
if((sock_serv = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
printf("socket creation error");
exit(-1);
}
bzero(&stru, sizeof(stru));
stru.sun_family = AF_UNIX;
strncpy (stru.sun_path, SERVER_PATH, sizeof(stru.sun_path));
if((bind(sock_serv, (struct sockaddr*) &stru, sizeof(stru))) < 0){
perror("bind error");
exit(EXIT_FAILURE);
}
if(listen(sock_serv, SOMAXCONN) < 0){
perror("listen error");
exit(EXIT_FAILURE);
}
if((new_sock = accept(sock_serv, NULL, 0)) < 0){
perror("accept error");
exit(EXIT_FAILURE);
}
bufflen = read(new_sock, buff, N);
if (bufflen < 0) {
perror("read error");
}
else if (bufflen == 0) {
printf("Client disconnected\n");
}
else {
printf("Server got: %.*s\n", (int) bufflen, buff);
}
close(new_sock);
close(sock_serv);
unlink(SERVER_PATH);
return 0;
}
client
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <errno.h>
#include "thpool.h"
#include "functions.h"
#define SERVER_PATH "/tmp/server"
const char *msg = "Hello!";
int main(void){
int sock_cl;
struct sockaddr_un sa;
ssize_t sent;
if((sock_cl = socket(AF_UNIX, SOCK_STREAM, 0)) < 0){
perror("socket creation error");
exit(EXIT_FAILURE);
}
bzero(&s, sizeof(sa));
sa.sa_family = AF_UNIX;
strncpy (sa.sa_data, SERVER_PATH, sizeof(sa.sa_data));
if (connect(sock_cl, (struct sockaddr*) &sa, (socklen_t) sizeof(sa)) < 0) {
perror("connect error");
exit(EXIT_FAILURE);
}
sent = write(sock_cl, msg, strlen(msg)+1);
if (sent < 0) {
perror("write error");
}
else {
printf("Message sent: %.*s\n", (int) sent, msg);
}
close(sock_cl);
return 0;
}
I would like to reuse one abstract socket adress during one server session. I tried to close sockets and bind them again, but it isn't working.
server.c:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
int main()
{
struct sockaddr_un workmanaddr;
workmanaddr.sun_family=AF_UNIX;
strcpy(workmanaddr.sun_path+1,"name");
workmanaddr.sun_path[0]='0';
char buf[255];
int sd = socket(AF_UNIX, SOCK_DGRAM, 0);
if(sd <0)
{
perror("socket() error");
exit(2);
}
if((bind(sd , (struct sockaddr *)&workmanaddr,sizeof(workmanaddr)))<0)
{
perror("bind() error");
exit(3);
}
while(true)
{
recv(sd, buf, sizeof(buf), 0);
printf("%s\n",buf);
close(sd);
sd = socket(AF_UNIX, SOCK_DGRAM, 0);
if(sd <0)
{
perror("socket() error");
exit(2);
}
if((bind(sd , (struct sockaddr*)&workmanaddr,sizeof(workmanaddr)))<0)
{
perror("bind() error");
exit(3);
}
}
return 0;
}
client.c:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
int main(int argc, char *argv[])
{
char _path[108]="name";
struct sockaddr_un tempadres = {0};
int tmpsock;
tmpsock = socket(AF_UNIX, SOCK_DGRAM, 0);
if(tmpsock<0)
{
perror("socket() error");
exit(2);
}
tempadres.sun_family=AF_UNIX;
strcpy(tempadres.sun_path+1,_path);
tempadres.sun_path[0]='0';
if((connect(tmpsock, (struct sockaddr *)&tempadres,sizeof(tempadres)))<0)
{
perror("connect() error");
exit(3);
}
char buf[255];
scanf("%s",buf);
if(send(tmpsock, buf, sizeof(buf), 0)<0)
{
perror("send() error");
exit(4);
}
return 0;
}
What should I do to make it possible to reuse it many times? Any advices?
In this line: tempadres.sun_path[0]='0','0' is not null byte what abstract socket requires for the first byte of the sun_path field. So it wouldn't be regarded as abstract socket. Change it to tempadres.sun_path[0]=0 or tempadres.sun_path[0]='\0'.
PS: there is another problem in your server.c, you didn't zero out workmanaddr, so the sun_path is very likely to contain garbage value as it's allocated on the stack even though you called strcpy(workmanaddr.sun_path+1,"name"); and a null byte is appended because all the remaining bytes in sun_path define the "name" of the socket. it would lead to different socket names with the client, resulting in connection refused on client side.
I am writing a simple client server program using unix domain sockets, but am having issues with the recv() call in my client program.
The program executes as follows:
Server sets up socket and waits for a connection
Client connects and sends a string
Server receives string, and sends string back to client (like an echo)
Client recv() call fails, returning "resource temporarily unavailable"
Client exits
Server waits for another connection
I have also tried using a poll() call in my client to wait for the response from the server.
In this case however, the recv() call simply receives a 0, implying the connection has been closed serverside, which it has not.
I have exhausted google on this error, but no fixes I came accross seem applicable to my code.
I have included my client (with poll() code commented out) and server code below.
I'm probably missing something obvious... but any insight would be greatly appreciated!
Server code:
/*
* testServer.c
*
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/un.h>
#include <linux/spi/spidev.h>
#include <linux/sockios.h>
#include <errno.h>
#define SOCK_PATH "/var/run/ts.serv"
void handleSockIO(int *sockDesc);
int main ()
{
int sock;
struct sockaddr_un sock_addr;
int len, p;
struct pollfd poll_fd[1];
printf("[TS] testServer Started.\r\n");
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
{
perror("[TS]wr_sock creation");
}
else
{
printf("[TS] Created socket descriptor.\r\n");
}
sock_addr.sun_family = AF_UNIX;
strcpy(sock_addr.sun_path, SOCK_PATH);
unlink(sock_addr.sun_path);
len = strlen(sock_addr.sun_path) + sizeof(sock_addr.sun_family);
if (bind(sock, (struct sockaddr *)&sock_addr, len) == -1)
{
perror("[TS]sock bind failed\r\n");
}
else
{
printf("[TS] Bound socket to sock_addr.\r\n");
}
if (listen(sock, 5) == -1)
{
perror("[TS] sock listen fail");
}
else
{
printf("[TS] Socket now listening.\r\n");
}
poll_fd[0].fd = sock;
poll_fd[0].events = POLLIN;
printf("[TS] Waiting for a connection...\r\n");
while (1)
{
p = poll(poll_fd, 1, 1); //Wait for 1 ms for data
if (p == -1)
{
perror("[TS] Poll");
}
else if (p == 0)
{
//printf("Timeout occurred!\n");
}
else
{
if (poll_fd[0].revents & POLLIN)//Data available to read without blocking
{
printf("[TS] Data available on sock..\r\n");
handleSockIO(&sock);
printf("[TS] Waiting for another connection...\r\n");
}
}
}//While(1)
return 0;
}
void handleSockIO(int *sockDesc)
{
int ioSock, n;
socklen_t t;
struct sockaddr_un remote_addr;
char str[15];
memset(str, ' ', sizeof(str));
t = sizeof(remote_addr);
if ((ioSock = accept(*sockDesc, (struct sockaddr *)&remote_addr, &t)) == -1)
{
perror("accept failed\r\n");
}
else
{
printf("[TS] Receiving...\r\n");
n = recv(ioSock, str, sizeof(str), 0);
if (n < 0)
printf("[TS] recvfrom failed: %s\r\n", strerror(errno));
else if(n == 0)
{
printf("[TS] Received %d on ioSock...\r\n", n);
}
else if(n > 0)
{
printf("[TS] Received: %s, which is %d long.\r\n", str, strlen(str));
printf("[TS] Echoing response...\r\n");
if (send(ioSock, str, n, 0) == -1) //Echo str back
{
printf("[TS] send failed: %s\r\n", strerror(errno));
}
else
{
printf("[TS] Send successful\r\n");
}
//============Wait to close IO descriptor=================
int r;
char temp[1]; //Arbitrary buffer to satisfy recv()
do
{
printf("[TS] Waiting for client to close connection...\r\n");
r = recv(ioSock, temp, sizeof(temp), 0);
if (r == 0)
{
printf("[TS] Client closed connection, closing ioSock...\r\n");
close(ioSock);
}
} while (r != 0);
//========================================================
}//if(n>0) else...
}
}
Client code:
/*
* testClient.c
*
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/un.h>
#include <linux/sockios.h>
#include <errno.h>
#define CHAR_BUF_SIZE 15
#define SEND_STRING "Hello!"
#define SOCK_PATH "/var/run/ts.serv"
int main ()
{
char str[CHAR_BUF_SIZE] = {0};
int c, len, n, p;
int s; // s will hold a socket descriptor returned by socket()
struct sockaddr_un serv_addr;
struct pollfd poll_fd[1];
printf("[TC] testClient Started.\r\n");
//===============SOCKET SETUP===============================
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
{
printf("[TC] Socket failed: %s\r\n", strerror(errno));
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path, SOCK_PATH);
len = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
//==========================================================
// printf("[TC]Trying to connect to TS socket...\r\n");
//===============RESPONSE POLL SETUP========================
poll_fd[0].fd = s;
poll_fd[0].events = POLLIN;
//==========================================================
printf("[TC] Connecting to SOCK_PATH...\r\n");
c = connect(s, (struct sockaddr *)&serv_addr, len);
if (c == -1)
{
printf("[TC] Connection failed: %s\r\n", strerror(errno));
}
else
{
printf("[TC] Connected. Sending string....\r\n");
if (send(s, SEND_STRING, strlen(SEND_STRING), 0) == -1)
{
printf("[TC] send() failed: %s\r\n", strerror(errno));
}
else
{
printf("[TC] Send on SOCK_PATH successful.\r\n");
//Sending complete------------------------------------------------
//Wait for response...
printf("[TC] Waiting for server response...\r\n");
// p = poll(poll_fd, 1, -1); //Wait for a response
//
// if (p == -1)
// {
// perror("[TC] Poll");
// }
// else
// {
// if(poll_fd[0].revents & POLLIN)
// {
n = recv(s, str, sizeof(str), 0);
if (n < 0)
{
printf("[TC] Receive on SOCK_PATH failed: %s\r\n",
strerror(errno));
}
else if(n == 0)
{
printf("[TC] %d Received on SOCK_PATH.\r\n", n);
}
else if(n > 0)
{
printf("[TC] Received %d from SOCK_PATH: %s\r\n",
n, str);
}
// }
// }
}//if(send())
}//if(connect())
printf("[TC] Transction complete, closing connection and exiting.\r\n");
close(s);
return 0;
}
len = sizeof(serv_addr) instead of len = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family) should solve you problem. Also do not ignore compiler warnings, say n = recv(s, str, strlen(str), 0) with n declared as int and ssize_t returned by recv. It will help you to avoid a future errors.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
int main()
{
int sock, newsock;
struct sockaddr server_name = {AF_UNIX, "Fred"};
socklen_t len=sizeof(struct sockaddr)+5;
if( (sock=socket(AF_UNIX,SOCK_STREAM,0)) ==-1)
{
printf("error creating socket");
return -1;
}
if( bind(sock,&server_name,len) != 0 ) //binding
{
printf("socket bind error ");
return -1;
}
if(listen(sock,10)!=0) //set sock to listen
{
printf("error listening");
return -1;
}
printf("Waiting for connections....");
while(1)
{
newsock=accept(sock, (struct sockaddr*)NULL, NULL);
char temp[1000]="gggggg\n";
write(newsock,temp,strlen(temp));
write(newsock,temp,strlen(temp));
close(newsock);
sleep(1);
}
return 0;
}
"Waiting for connections..." doesn't appear on my screen. What's the problem? I tried print function everywhere. Nothing shows up..What is the problem? I don't see that I closed stdout..Can anyone help? Thank you.
The standard output is line buffered by default. Your program outputs something without the new line and then enters the loop. You need to add a new line like this:
printf("Waiting for connections....\n");
For similar reasons, the error messages should be output to standard error, because it's not buffered.
fprintf(stderr, "error creating socket\n");
Firstly, you should add '\n' add the end of log:"Waiting for connections....\n", then the log cann't be buffered, and be outputted.
Secondly, your program is error, the correct program is like this:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
int main()
{
int sock, newsock;
struct sockaddr_un server_name;
socklen_t len= sizeof(server_name);
unlink("Fred");
if( (sock=socket(AF_UNIX,SOCK_STREAM,0)) ==-1)
{
printf("error creating socket\n");
return -1;
}
server_name.sun_family = AF_UNIX;
strcpy(server_name.sun_path, "Fred");
if( bind(sock,(struct sockaddr*)&server_name,len) != 0 ) //binding
{
printf("socket bind error \n");
return -1;
}
if(listen(sock,10)!=0) //set sock to listen
{
printf("error listening\n");
return -1;
}
printf("Waiting for connections....\n");
while(1)
{
newsock=accept(sock, (struct sockaddr*)NULL, NULL);
char temp[1000]="gggggg\n";
write(newsock,temp,strlen(temp));
write(newsock,temp,strlen(temp));
close(newsock);
sleep(1);
}
return 0;
}
The server has to echo the message sent by the client using C program in Linux.I'm using Ubuntu OS (I don't know whether this information is useful or not!). It worked for the first time. But for the second time, it gave 'Error Connection'. I tried changing port numbers. But still it didn't work. Kindly guide me. I'm a beginner.
server.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
int main()
{
int sd, sd1, len, confd, n;
struct sockaddr_in ser, cli;
char msg[50];
if((sd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
printf("\nSocket creation error\n");
bzero(&ser, sizeof(ser));
ser.sin_family = cli.sin_family = PF_INET;
ser.sin_port = htons(10000);
ser.sin_addr.s_addr = htonl(INADDR_ANY);
len = sizeof(ser);
if ((bind(sd, (struct sockaddr*)&ser, len)) < 0) {
printf("\nBind Error");
exit(0);
}
if (listen(sd, 2) == 0) {
if ((sd1 = accept(sd, (struct sockaddr*)&ser, &len)) > 0) {
do {
bzero(&msg, 50);
read(sd1, msg, 50);
//int m=(int)msg;
printf("\nMessage from client:%s\n", msg);
write(sd1, msg, strlen(msg));
if(strcmp(msg, "exit") == 0)
break;
} while(strcmp(msg, "exit") != 0);
}
}
}
*strong text*client.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <netinet/in.h>
int main()
{
int sd, n, len;
struct sockaddr_in ser, cli;
char text[50];
if ((sd = socket(AF_INET,SOCK_STREAM, 0)) < 0)
printf("\nSocket creation error\n");
bzero(&ser, sizeof(ser));
ser.sin_family = cli.sin_family = PF_INET;
ser.sin_port = htons(10000);
ser.sin_addr.s_addr = htonl(INADDR_ANY);
len = sizeof(ser);
if ((connect(sd, (struct sockaddr*)&ser, len)) < 0) {
printf("\nError connection");
exit(0);
}
while(1) {
strcpy(text, " ");
printf("\nEnter data which is to be sent:");
scanf("%s", text);
write(sd, text, strlen(text));
read(sd, text, 50);
printf("\nEcho msg from server:%s", text);
if (strcmp(text, "exit") == 0)
break;
}
close(sd);
}
Can your client really connect to any address?
ser.sin_addr.s_addr=htonl(INADDR_ANY);
Most likely you meant to connect to a specific server:
ser.sin_addr.s_addr=inet_addr("127.0.0.1");