EDIT: Minimal, Complete, and Verifiable example is in comment lower and the code actually works, problem was in different area. Sorry for bad posting, I cannot delete it now.
I know, that there are some pages about this already, but I really tried everything and nothing works. Im having this error all the time and my code looks like this:
int server_socket, new_socket;
struct sockaddr_in server;
bzero((char *) &server, sizeof(server));
server_socket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if(server_socket < 0) error("ERROR opening socket");
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(port_number);
if(bind(server_socket, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0) error("ERROR on binding");
if(listen(server_socket, 5) < 0) error("Failed to listen for connections.");
while(1)
{
struct sockaddr_in client;
unsigned int client_len = sizeof(struct sockaddr_in);
bzero((char *) &client, client_len);
if((new_socket = accept(server_socket, (struct sockaddr *) &client, &client_len)) < 0)
{
if(errno == EAGAIN) continue;
else error("ERROR on accept");
}
if((pid = fork()) > 0)
{ // this is parent process
close(new_socket);
}
else if(pid == 0)
{ // this is a child process that will handle an incoming request
long_pid = (long) getpid(); // current child's PID
close(server_socket);
server_socket = -1; //closing parent socket
printf("A new connection accepted from blablabla, port %d by process %ld\n", port_number, long_pid);
// ---- doing some staff, running the program
EDIT: while((msg_size = read(new_socket, received_data, BUFFER_SIZE)) >0)
// ---- when Im done:
printf("closing newsock\n");
close(new_socket); // close the new socket
new_socket = -1;
exit(0);
}
}
else error("fork() failed");
}
// close the server
printf("closing an original socket\n");
close(server_socket); // close an original server socket
return 0;
}
I didnt have this problem before, when I was using nonblocking sockets. So problem is probably connected to them.
If I fill in the blanks to make this into a compilable example (please post an mcve next time)
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <errno.h>
#include <arpa/inet.h>
#define port_number 2222
void error(char const *Msg)
{
perror(Msg);
exit(1);
}
int main()
{
pid_t pid; long long_pid;
int server_socket, new_socket;
struct sockaddr_in server;
bzero((char *) &server, sizeof(server));
server_socket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if(server_socket < 0) error("ERROR opening socket");
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(port_number);
if(bind(server_socket, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0) error("ERROR on binding");
if(listen(server_socket, 5) < 0) error("Failed to listen for connections.");
while(1)
{
struct sockaddr_in client;
unsigned int client_len = sizeof(struct sockaddr_in);
bzero((char *) &client, client_len);
if((new_socket = accept(server_socket, (struct sockaddr *) &client, &client_len)) < 0)
{
if(errno == EAGAIN) continue;
else error("ERROR on accept");
}
if((pid = fork()) > 0)
{ // this is parent process
close(new_socket);
}
else if(pid == 0)
{ // this is a child process that will handle an incoming request
long_pid = (long) getpid(); // current child's PID
close(server_socket);
server_socket = -1; //closing parent socket
printf("A new connection accepted from blablabla, port %d by process %ld\n", port_number, long_pid);
// ---- doing some staff, running the program
// ---- when Im done:
printf("closing newsock\n");
close(new_socket); // close the new socket
new_socket = -1;
_exit(0);
}
else error("fork() failed");
}
// close the server
printf("closing an original socket\n");
close(server_socket); // close an original server socket
return 0;
}
it seems to work without any issues.
Obviously you're establishing the listening socket with error checks in the parent process and the parent process then isn't doing anything with the listening socket that closes it, so you shouldn't be getting that error from the parent process.
My guess is you're probably mistakenly continuing the loop in the child (in the part you're not showing).
Related
I try to make a application that client send a message and the server respond on the same machine using winsock TCP c++. The problem is that after the server waited for client to connect, I ran the client code and it stopped at the connect and exit. This is my code.
Server:
#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <iostream>
#define MY_PORT 8888
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Init Winsock
WSADATA SData;
if (WSAStartup(0x0202, &SData) != 0)
{
cout << "KHONG THE KHOI DONG WINSOCK";
return 1;
}
//Init Socket
int listeningSocket;
if ((listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
perror("socket failed");
WSACleanup();
exit(EXIT_FAILURE);
}
//Set IP and PORT
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(MY_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
//Bind
if (bind(listeningSocket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("bind failed");
closesocket(listeningSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
//Listen
if (listen(listeningSocket, 5) < 0)
{
perror("listen failed");
closesocket(listeningSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
//Accept
int new_socket;
sockaddr_in client_addr;
int nSizeAddr = sizeof(sockaddr);
if (new_socket = accept(listeningSocket, (struct sockaddr *)&client_addr, &nSizeAddr) < 0)
{
perror("accept failed");
closesocket(listeningSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
while (1)
{
//recv
char buff[100];
if (recv(new_socket, buff, 100, 0) < 0)
{
perror("recv failed");
closesocket(listeningSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
cout << buff << endl;
//send
string sndStr = "Da nhan";
if (send(new_socket, sndStr.c_str(), sndStr.size(), 0) < 0)
{
perror("send failed");
closesocket(listeningSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
}
//close socket
closesocket(new_socket);
//Cleanup winsock
WSACleanup();
return 0;
}
Client:
#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <iostream>
#include <string>
#define MY_PORT 8888
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Init winsock
WSADATA SData;
if (WSAStartup(0x0202, &SData) != 0)
{
cout << "KHONG THE KHOI DONG WINSOCK";
return 1;
}
//Init socket
int clientSocket;
if (clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) < 0)
{
perror("socket failed");
WSACleanup();
exit(EXIT_FAILURE);
}
//Set IP and PORT
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(MY_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY; //Vi Client-Server chung 1 may
//Connect
if (connect(clientSocket, (SOCKADDR*)&server_addr, sizeof(server_addr)) < 0)
{
perror("connect failed");
closesocket(clientSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
while (1)
{
//Send
string sndStr;
getline(cin, sndStr);
if (send(clientSocket, sndStr.c_str(), sndStr.size(), 0) < 0)
{
perror("send failed");
closesocket(clientSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
//Recv
char buff[100];
if (recv(clientSocket, buff, 100, 0) < 0)
{
perror("recv failed");
closesocket(clientSocket);
WSACleanup();
exit(EXIT_FAILURE);
}
cout << buff << endl;
}
//Close socket
closesocket(clientSocket);
//Cleanup Winsock
WSACleanup();
}
It had an error at the accept step but I don't know what was the error name. How can I fix it?
The client_socket value turn out INVALID_SOCKET.
EDIT: New problem found - I still don't believe that using INADDR_ANY as a client address is good, but that isn't actually the problem here.
Instead the problem is in the line:
if (clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP) < 0)
Note that < has higher precedence than =, so you assign a boolean value to clientSocket here. The similar line in the server code has parenthesis added that makes it work correctly.
Working line in the server code:
if ((listeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
OLD ANSWER: I still stand by this, but it isn't the problem here
.
This is your problem right here (in the client):
server_addr.sin_addr.s_addr = INADDR_ANY; //Vi Client-Server chung 1 may
The way sockets work, is that you create a server socket that listens for incoming connections, and on creation you tell it who to accept connections from. Either from a specific address, a range of addresses, or just from anybody. The third option is the most common one here, and the one you have chosen in your server (bind to address INADDR_ANY).
This part is good.
However, when the client has to connect to the server, you cannot just say: "Connect to whatever!" and expect it to find your server (or anything at all). So when you try to connect with the client socket you have to give it the actual address of the server.
I found a example of how to do this:
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr*) & serv_addr, sizeof(serv_addr)) < 0)
Change the address to the correct one for your server, if localhost (127.0.0.1) don't work for you.
I am just learning UDP sockets and this is my first code involving it. I have two programs which send and receive messages back and forth. My question is it seems I have to declare which IP address I am sending/receiving from multiple times throughout the code as it changes but I feel there is a better way to do this without changing the inet_addr manually within the codes. From my reading it looks like sendto and recvfrom may be able to help but I am unsure how to use them in this context. If anyone could show me how to fix my simple problem I would greatly appreciate it! Thanks
CODE 1: Send then Receive
int main(int argc, char *argv[])
{
//initialize socket and structure
int socket_info;
struct sockaddr_in server;
char message[100];
char incoming_message[100];
printf("Input Message: ");
fgets(message, 100, stdin);
//create socket
socket_info = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_info == -1) {
printf("Could not create socket");
}
//assign local values
server.sin_addr.s_addr = inet_addr("172.21.8.178");
server.sin_family = AF_INET;
server.sin_port = htons( 1100 );
//binds connection
if (bind(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("Connection error");
return 1;
}
puts("Bind");
//assign new value to connect to
server.sin_addr.s_addr = inet_addr("172.21.8.179");
//checks connection
if (connect(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("Connection error");
return 1;
}
puts("Connected");
//sends message
if(send(socket_info, message, strlen(message), 0) <0) {
perror("Send failed");
return 1;
}
puts("Message Sent");
//receives message back
if(recv(socket_info, incoming_message, sizeof(incoming_message), 0) <0) {
puts("Received failed");
return 1;
}
puts("Message received");
puts(incoming_message);
close(socket_info);
}
CODE 2: Receive then Send
int main(int argc, char *argv[])
{
//initialize socket and structure
int socket_info;
struct sockaddr_in server;
char incoming_message[100];
//create socket
socket_info = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_info == -1) {
printf("Could not create socket");
}
//assign values
server.sin_addr.s_addr = inet_addr("172.21.8.179");
server.sin_family = AF_INET;
server.sin_port = htons( 1100 );
//checks connection
if (bind(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("Connection error");
return 1;
}
puts("Bind");
//Receive an incoming message
if( recv(socket_info, incoming_message, sizeof(incoming_message), 0) < 0) {
puts("Received failed");
return 1;
}
puts("Message received");
puts(incoming_message);
server.sin_addr.s_addr = inet_addr("172.21.8.178");
if (connect(socket_info, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("Connection error");
return 1;
}
puts("Connected");
//Sends message back
char message[100];
printf("Input Message: ");
fgets(message, 100, stdin);
if(send(socket_info, message, strlen(message), 0) <0) {
perror("Send failed");
return 1;
}
puts("Message Sent");
close(socket_info);
}
If you use the function recvfrom()
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
What this function does is it fills a structure of sockaddr with the IP and port information of the packet that it has just received. For example, if your code that sends first then receives sends a packet to the receiver, the receiver should be able to fill the structure values of sin_addr and sin_port with the correct values. You can then make a call of sendto() with this information in order to send it to the correct machine.
Here's the man pages for these functions:
https://linux.die.net/man/2/recvfrom
https://linux.die.net/man/2/sendto
Try using this:
inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)
127.0.0.1 is the loopback IP. The address is used to establish an IP connection to the same machine, which seems to be your case.
A detailed way to solve the problem can be found here
I've wrote a simple client code in python, and I'm trying to connect to a simple echo server written in C.
I know it shouldn't matter, but for some reason I did manage to connect to a server written in python, but I cannot connect to the C server.
Here's the code of the client:
import socket
import sys
import time
HOST = 'localhost'
PORT = 11000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
msg = raw_input()
s.send(msg)
data = s.recv(len(msg))
s.close()
print 'Received: ', data
And here's the C code of the echo server:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#ifndef AF_INET
#define AF_INET 2
#endif
#ifndef SOCK_DGRAM
#define SOCK_DGRAM 2
#endif
#ifndef INADDR_ANY
#define INADDR_ANY 0
#endif
#ifndef IP_DONTFRAG
#define IP_DONTFRAG 67
#endif
#define BUFFER_SIZE 1024
#define ECHO_PORT_UDP 10000
#define ECHO_PORT_TCP 11000
int main(int argc, char *argv[]) {
int echo_socket = 0;
int echo_socket_child = 0; // for TCP
struct sockaddr_in server;
struct sockaddr_in client;
struct hostent *hostp; // client host info
struct sockaddr_in clientaddr; // client addr
char *hostaddrp; // dotted decimal host addr string
char buffer[BUFFER_SIZE];
unsigned int clientlen = 0;
unsigned int serverlen = 0;
int received = 0;
int port = 0;
char *endptr;
int optval = 1;
int msg_byte_size = 0;
// Parameters check
if (argc == 2) {
port = strtol(argv[1], &endptr, 0);
if ((*endptr) || ((port != ECHO_PORT_UDP) && (port != ECHO_PORT_TCP))) {
printf("EchoServer: Invalid port number.\n Use port %d for UDP, port %d for TCP.\n", ECHO_PORT_UDP, ECHO_PORT_TCP);
return -1;
}
else {
if (port == ECHO_PORT_UDP) {
printf("EchoServer: Running UDP on port %d.\n", port);
}
if (port == ECHO_PORT_TCP) {
printf("EchoServer: Running TCP on port %d.\n", port);
}
}
}
else {
printf("EchoServer: Invalid arguments.\n");
return -1;
}
// Opening UDP socket
if (port == ECHO_PORT_UDP) {
if ((echo_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
printf("EchoServer: Failed opening socket");
return -1;
}
}
if (port == ECHO_PORT_TCP) {
if ((echo_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("EchoServer: Failed opening socket");
return -1;
}
// setsockopt: Handy debugging trick that lets us rerun the server immediately after we kill it; otherwise we have to wait about 20 secs.
// Eliminates "ERROR on binding: Address already in use" error.
setsockopt(echo_socket, SOL_SOCKET, SO_REUSEADDR,(const void *)&optval , sizeof(int));
}
// Construct the server sockaddr_in structure
memset(&server, 0, sizeof(server)); /* Clear struct */
server.sin_family = AF_INET; /* Internet/IP */
server.sin_addr.s_addr = htonl(INADDR_ANY); /* Any IP address */
server.sin_port = htons(atol(argv[1])); /* server port */
// Bind the socket
serverlen = sizeof(server);
if (bind(echo_socket, (struct sockaddr *) &server, serverlen) < 0) {
printf("EchoServer: Failed binding socket");
return -1;
}
// Wait for a datagram until cancelled
if (port == ECHO_PORT_UDP) {
while (1) {
/* Receive a message from the client */
clientlen = sizeof(client);
if ((received = recvfrom(echo_socket, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client, &clientlen)) < 0) {
printf("EchoServer: Failed receiving datagram");
return -1;
}
printf("Client datagram received from: %s\n", inet_ntoa(client.sin_addr));
/* Send the message back to client */
if (sendto(echo_socket, buffer, received, 0, (struct sockaddr *) &client, sizeof(client)) != received) {
printf("Mismatch in number of echoed bytes");
return -1;
}
}
}
// Wait for a connection until cancelled
if (port == ECHO_PORT_TCP) {
while (1) {
echo_socket_child = accept(echo_socket, (struct sockaddr *) &client, &clientlen);
if (echo_socket_child < 0) {
printf("ERROR on accept");
break;
}
// gethostbyaddr: determine who sent the message
hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET);
if (hostp == NULL) {
printf("ERROR on gethostbyaddr");
break;
}
hostaddrp = inet_ntoa(clientaddr.sin_addr);
if (hostaddrp == NULL) {
printf("ERROR on inet_ntoa\n");
break;
}
printf("server established connection with %s \n", hostaddrp);
// read: read input string from the client
bzero(buffer, BUFFER_SIZE);
msg_byte_size = read(echo_socket_child, buffer, BUFFER_SIZE);
if (msg_byte_size < 0) {
printf("ERROR reading from socket");
break;
}
printf("server received %d bytes: %s", msg_byte_size, buffer);
// write: echo the input string back to the client
msg_byte_size = write(echo_socket_child, buffer, strlen(buffer));
if (msg_byte_size < 0) {
printf("ERROR writing to socket");
break;
}
} // endof while(1)
close(echo_socket_child);
return -1;
}
return 0;
}
Any ideas why I fail to connect to the server?
edit:
this is the error I receive:
Traceback (most recent call last):
File "s.py", line 8, in <module>
s.connect((HOST, PORT))
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10061]
(1) Add a listen call to the TCP section of the code.
(2) You have to tell accept what the length of the sockaddr you are passing it is and it will in return tell you the length of the address of the client it returned. You were passing it as 0 length so naturally it didn't pass back a client address which subsequently makes your gethostbyaddr fail with unknown address.
(3) If you don't close the client socket within the loop it just remains open (and leaks a file descriptor) for the duration of the server's life. Eventually you will run out of FDs. It doesn't effect your client which just closes after the receipt of one msg but any client who writes more than one message will never have it received by the server and will never receive an eof from the server.
if (port == ECHO_PORT_TCP)
{
if (listen(echo_socket, ECHO_PORT_TCP) == -1)
{
perror("listen");
exit(1);
}
while (1)
{
clientlen = sizeof(client);
echo_socket_child = accept(echo_socket, (struct sockaddr *) &client, &clientlen);
if (echo_socket_child < 0)
{
perror("accept");
break;
}
// gethostbyaddr: determine who sent the message
hostp = gethostbyaddr((const char *) &client.sin_addr.s_addr, sizeof(client.sin_addr.s_addr), AF_INET);
if (hostp == NULL)
{ herror("byaddr");
break;
}
hostaddrp = inet_ntoa(client.sin_addr);
if (hostaddrp == NULL)
{
printf("ERROR on inet_ntoa\n");
break;
}
printf("server established connection with %s (%s)\n", hostp->h_name, hostaddrp);
bzero(buffer, BUFFER_SIZE);
msg_byte_size = read(echo_socket_child, buffer, BUFFER_SIZE);
if (msg_byte_size < 0)
{
printf("ERROR reading from socket");
break;
}
printf("server received %d bytes: %s", msg_byte_size, buffer);
msg_byte_size = write(echo_socket_child, buffer, strlen(buffer));
if (msg_byte_size < 0)
{
printf("ERROR writing to socket");
break;
}
close(echo_socket_child);
} // endof while(1)
return -1;
}
I have a client - server model using 2 C++ files. The client and server communicate through a socket. The user enters strings to the client's stdin and then the client passes that to the server. However, I want to simulate the user using a python program. This is my attemp:
from subprocess import Popen, PIPE, STDOUT
host_name = raw_input("Enter host name: ")
port = raw_input("Enter port: ")
p = Popen(["./client", host_name, port], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
p.stdin.write("DEVICE:3:MALFUNCTIONING")
This doesn't work. It starts the client process and the arguments, but does not write the string DEVICE:3:MALFUNCTIONING to it. Any suggestion?
This is briefly my client code:
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
memset(buffer, 0, 256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
memset(buffer, 0, 256);
n = read(sockfd,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s\n",buffer);
close(sockfd);
I am not sure how you create your client and server, but here's my implementation which is pretty much the same as yours for the python program simulating the user.
Github gist: https://gist.github.com/yanhan/5791613
I have a few suspicions of what may be going wrong based on your code snippets for the client:
server_addr variable not set properly
bug in the server code
Like mata said, it'll be more helpful if you could show any output from stdout/stderr and stacktraces.
EDIT: Just wanted to add that the following links might help:
http://en.wikibooks.org/wiki/C_Programming/Networking_in_UNIX
http://rabbit.eng.miami.edu/info/functions/internet.html#clientsample
http://rabbit.eng.miami.edu/info/functions/internet.html#serversample
I am pasting my code here for convenience.
sim.py:
from subprocess import Popen, PIPE
def run():
# this should work if you change to
# host_name = raw_input
# port = raw_input
host_name = '127.0.0.1'
port = '8124'
p = Popen(['./client', host_name, port], stdin=PIPE, close_fds=True)
myInput = raw_input()
p.stdin.write(myInput)
p.communicate()
if __name__ == '__main__':
run()
client.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFSZ 256
int main(int argc, char *argv[])
{
int sockfd, ret, exitCode = 0;
char buffer[BUFSZ];
memset(buffer, 0, BUFSZ);
fgets(buffer, sizeof(buffer), stdin);
char *hostname = argv[1];
int port = atoi(argv[2]);
// echo out the stuff just to see
printf("hostname = %s\n", hostname);
printf("port = %d\n", port);
printf("msg = %s\n", buffer);
// creates the socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
fprintf(stderr, "client: socket() failed\n");
exit(1);
}
struct sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(struct sockaddr_in));
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(port);
sockAddr.sin_addr.s_addr = inet_addr(hostname);
// connect to server
ret = connect(sockfd, (const struct sockaddr *)&sockAddr, sizeof(struct sockaddr));
if (ret == -1) {
fprintf(stderr, "client: connect failed\n");
exitCode = 1;
goto done;
}
// send buffer
ssize_t charsSent = send(sockfd, buffer, strlen(buffer)+1, 0);
printf("client: chars sent: %d\n", (int)charsSent);
/*
// this seems to work as well
write(sockfd, buffer, strlen(buffer)+1);
*/
done:
close(sockfd);
return exitCode;
}
server.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORTNUM 8124
#define BUFSZ 256
int main(int argc, char *argv[])
{
char buf[BUFSZ];
int ret, exitCode = 1;
// create socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
int recvFd;
if (sockfd == -1) {
fprintf(stderr, "server: Error creating socket\n");
exit(1);
}
struct sockaddr_in sockAddr;
memset(&sockAddr, 0, sizeof(struct sockaddr_in));
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(PORTNUM);
sockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
// bind socket
ret = bind(sockfd, (const struct sockaddr *)&sockAddr, sizeof(struct sockaddr));
if (ret == -1) {
fprintf(stderr, "server: bind failed\n");
exitCode = 1;
goto done;
}
// listen for connection. max of 1 connection
ret = listen(sockfd, 1);
if (ret == -1) {
fprintf(stderr, "server: listen failed\n");
exitCode = 1;
goto done;
}
struct sockaddr_in dest;
socklen_t sockLen;
ssize_t bytesReceived;
while (1) {
// accept connection from client
recvFd = accept(sockfd, (struct sockaddr *)&dest, &sockLen);
if (recvFd == -1) {
fprintf(stderr, "server: accept failed\n");
exitCode = 1;
break;
}
// receive message from client
bytesReceived = recv(recvFd, buf, BUFSZ, 0);
if (bytesReceived == -1) {
fprintf(stderr, "server: recv failed\n");
exitCode = 1;
break;
} else {
printf("server: received %s", buf);
fflush(stdout);
}
}
done:
close(sockfd);
return exitCode;
}
I am sorry if the title is misleading. I cannot really think of a better one. Here is some simple socket program that I have:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/poll.h>
#include <iostream>
#define BACKLOG 10
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
struct sockaddr_in serv_addr;
bzero((char *) &serv_addr, sizeof(serv_addr));
int portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
auto bindsuccess = bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
if (bindsuccess < 0)
error("ERROR on binding");
struct pollfd newPollFD[1];
newPollFD[0].fd = sockfd;
newPollFD[0].events = POLLIN;
char buffer[256];
bzero(buffer,256);
listen(sockfd, BACKLOG);
struct sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr);
int newsockfd;
int rv = poll(newPollFD, 1, -1);
if (rv == -1) {
error("Error occured in Poll");
}
else {
if (newPollFD[0].revents & POLLIN){
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
int rwsuccess;
rwsuccess = read(newsockfd,buffer,255);
if (rwsuccess < 0)
error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
rwsuccess = write(newsockfd,"I got your message \n",18);
if (rwsuccess < 0)
error("ERROR writing to socket");
}
}
close(newsockfd);
close(sockfd);
return 0;
}
If I execute this code with a port number, and then telnet to that port number, the server listens in, but as soon as I send my first message, the connection terminates. I want the sever to keep on listening and sending acknowledgement back as many times as I send message from telnet. How do I accomplish that?
Thanks in advance!
The continue is curiously extraneous, and causes the compilation to break. (It is now corrected in your edit.) But, you are missing a for loop. And, you should add a check if the read returns 0.
for (;;) {
int rwsuccess;
rwsuccess = read(newsockfd,buffer,255);
if (rwsuccess < 0)
error("ERROR reading from socket");
if (rwsuccess == 0) break;
printf("Here is the message: %s\n",buffer);
rwsuccess = write(newsockfd,"I got your message \n",18);
if (rwsuccess < 0)
error("ERROR writing to socket");
}