I wanted to compile sample code for a UDP client on a Mac OS X, but got a following error:
client.c:91:9: error: use of undeclared identifier 'MSG_CONFIRM'
MSG_CONFIRM, (const struct sockaddr *) &servaddr,
^
I googled for the error for several hours but couldn't find anything related to this problem. Does anybody know what is the issue and how to fix it?
The code for UDP client is below
// Client side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#define PORT 8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from client";
struct sockaddr_in servaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY;
int n, len;
sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &servaddr,
sizeof(servaddr));
printf("Hello message sent.\n");
while (1)
{
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, (struct sockaddr *) &servaddr,
&len);
buffer[n] = '\0';
printf("Server : %s\n", buffer);
}
close(sockfd);
return 0;
}
My friend suggest me to define MSG_CONFIRM as 0. He said search what is use for it. You also research it.
#define MSG_CONFIRM 0
Related
I've been trying and trying to fragment my TCP packets but I havent found any helpful implementation of it. I am familiar with the theory and concepts of fragmentation, have even come across some flags such as IP_PMTUDISC_DONT, IP_PMTUDISC_WANT, and IP_PMTUDISC_DO and set them but the wireshark's capture always showed DF Flag as on.
I've set the MTU of my 'lo' Network Interface to 1500 since I'm using LoopBack Address on both, the server and the client. And I thought that fragmentation will be handled by the Network Layer, but thats not the case I guess...
Please help me with fragmentation of the packet.Here's my code...
Server.cpp
#include <sys/types.h>
#include <sys/socket.h>
#include <iostream>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netdb.h>
int main(){
struct sockaddr_in serv_addr, cli_addr;
char buff[255];
int FileDesc = socket(AF_INET,SOCK_STREAM, 0);
if(FileDesc < 0){
perror("Socket Creation Failed. ");
exit(EXIT_FAILURE);
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(5455);
if (bind(FileDesc, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
perror("Bind Failed.");
exit(EXIT_FAILURE);
}
if (listen(FileDesc, 6) < 0){
perror("Listen Failed.");
exit(EXIT_FAILURE);
}
socklen_t cliLen;
int AcceptFD = accept(FileDesc, (sockaddr*)&cli_addr, &cliLen);
if(AcceptFD < 0){
perror("Accept Failed.");
exit(EXIT_FAILURE);
}
//Setting the MTU
struct ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET;//address family
strncpy(ifr.ifr_name, "lo", sizeof(ifr.ifr_name));//interface name where you want to set the MTU
ifr.ifr_mtu = 1500; //your MTU size here
if (ioctl(FileDesc, SIOCGIFMTU, (caddr_t)&ifr) < 0){
perror("ioctl error.");
exit(1);
}
std::cout << ifr.ifr_ifru.ifru_mtu;
while(1){
//File Transfer
}
close(AcceptFD);
close(FileDesc);
return 0;
}
Client.cpp
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
int main(){
struct sockaddr_in serv_addr;
char buffer[255];
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0){
perror("Socket Creation Failed.");
exit(EXIT_FAILURE);
}
FILE *fp;
char *filename = "File.txt";
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
inet_aton("127.0.0.1", &serv_addr.sin_addr);
serv_addr.sin_port = htons(5455);
int conn = connect(fd, (const sockaddr*)&serv_addr, sizeof(serv_addr));
if (conn < 0){
perror("Connect Failed. ");
exit(EXIT_FAILURE);
}
while(1){
//File Recieve
}
close(fd);
return 0;
}
This also begs the question, how would I re-assemble my packet back in the same order?
Thank youu.
I coded a program in C of a tcp socket but once executed, the server returns an error from the accept function but i can't find out why..
If you guys can help me, that would be appreciated. Also, if you have any advice on the way i programmed this, please feel free to tell me.
Here is my code :
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
int listenSocket, status, socketClient;
unsigned short int msgLength;
struct addrinfo hints, *servinfo;
struct sockaddr_in clientAddress;
socklen_t clientAddressLength = sizeof clientAddress;
char msg[101];
//Test port number
if (argc != 2) {
fprintf(stderr,"Usage : %s [NUMERO_PORT]\n",argv[0]);
return 2;
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET; // IPv4
hints.ai_socktype = SOCK_DGRAM; // UDP
hints.ai_flags = 0; //Car on fait le test sur la meme machine
if ((status = getaddrinfo(NULL, argv[1], &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
return 3;
}
if ((listenSocket = socket(servinfo->ai_family, servinfo-
>ai_socktype, servinfo->ai_protocol)) == -1) {
perror("socket:");
return 4;
}
if (bind(listenSocket, servinfo->ai_addr, servinfo->ai_addrlen) ==
-1) {
close(listenSocket);
perror("bind:");
return 5;
}
listen(listenSocket,5);
int sizeOfSockAddr = sizeof(clientAddress);
socketClient= accept(listenSocket, NULL, NULL);
if (socketClient < 0) {
fprintf(stderr,"Erreur accept\n");
return 6;
}
freeaddrinfo(servinfo);
printf("Waiting for a client's request %s\n", argv[1]);
while (1) {
//some things
}
}`
I'm actually back to programming in C, and I want to code a UDP Client.
My problem is that I'm having an error when executing the sendto function... getting errno : 22 and the message error : Invalid argument
char query[1024];
int querySize = strlen(query);
SOCKADDR_IN dest = { 0 };
int destSize = sizeof dest;
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr('192.168.0.3');
dest.sin_port = htons(6000);
sendto(sock, query, querySize, 0, (SOCKADDR *) &dest, destSize)
Hope someone could help me?
Here is my full code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if defined (WIN32)
#include <winsock2.h>
typedef int socklen_t;
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(param) close(param)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#endif
int main(int argc, char *argv[]) {
#if defined (WIN32)
WSADATA WSAData;
WSAStartup(MAKEWORD(2,2), &WSAData);
#endif
char source_ip[15] = "192.168.0.20";
int source_port = 5000;
char query[1024];
printf("- Opening Socket\n");
SOCKET sock;
sock = socket(AF_INET, SOCK_DGRAM, 0);
if(sock == INVALID_SOCKET) {
perror("[ERROR] socket()");
exit(errno);
}
printf("- Configuring socket source to : [%s:%d]\n", source_ip, source_port);
SOCKADDR_IN source;
source.sin_family = AF_INET;
source.sin_addr.s_addr = inet_addr(source_ip);
source.sin_port = htons(source_port);
if(bind(sock, (SOCKADDR *)&source, sizeof(source)) == SOCKET_ERROR) {
perror("[ERROR] bind()");
exit(errno);
}
int querySize = strlen(query);
SOCKADDR_IN dest = { 0 };
int destSize = sizeof dest;
dest.sin_family = AF_INET;
printf("- Sending packets\n");
dest.sin_addr.s_addr = inet_addr('192.168.0.3');
dest.sin_port = htons(6000);
if(sendto(sock, query, querySize, 0, (SOCKADDR *) &dest, destSize) < 0) {
perror("[ERROR] sendto()");
printf("%d\n", errno);
exit(errno);
}
printf("\n\n##############################\n");
printf("Closing socket ...\n");
closesocket(sock);
#if defined (WIN32)
WSACleanup();
#endif
printf("Program finished.\n");
return 0;
}
Did you notice,
that query is not being initialized?
So strlen(query) might result in a "very long" buffer.
That would be a good candidate for an EINVAL.
I'm trying to learn some WinSock and stuck in place. I want to sent RAW packet (256 bytes) to another device in the network. This device is just a listener. I'm using VC++ 2010 and WinSock.
So there a code I'm working on:
// winsock.cpp : main project file.
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <string.h>
//#include <WinSock2.h>
#define WIN32_LEAN_AND_MEAN
using namespace System;
int main(array<System::String ^> ^args)
{
WORD RequiredVersion;
WSADATA WData;
SOCKET s;
struct sockaddr_in addr;
long val;
struct hostent *he;
char host[128];
int port;
printf("HOST: ");
scanf("%s",host);
printf("PORT: ");
scanf("%d",&port);
printf("Data to send: ");
scanf("%d",&val);
RequiredVersion = MAKEWORD(2, 0);
if (WSAStartup(RequiredVersion, &WData) != 0) {
printf("ERROR! WinSock2\n");
return 1;
}
//he = gethostbyaddr((char *) host, 4, AF_INET);
he = gethostbyname(host);
if (he == NULL) {
printf("host not found.\n");
system("PAUSE");
return 1;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = *((unsigned long*) he->h_addr);
s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
//SOCK_RAW, IPPROTO_TCP);
connect(s, (struct sockaddr*) &addr, sizeof(addr));
printf("SENDING %d TO %s\n",val,inet_ntoa(addr.sin_addr));
val = htonl(val);
send(s, (char*) &val, sizeof(long), 0);
printf("WAITING FOR RESPONSE...\n");
recv(s, (char*) &val, sizeof(long), 0);
val = ntohl(val);
printf("RE: %d\n", val);
closesocket(s);
WSACleanup();
system("PAUSE");
return 0;
}
The problems I've got:
I'm probably not creating a real RAW socket.
I think I should not use 'gethostbyname', but I don't know how to replace it for an const. IP
My program doesn't sent any data over LAN. I'm using WireShark to check this. I think this can be a problem with 'gethostbyname'
Any tips? I really appreciate any help you can provide!
I am writing a CLIENT/SERVER program and I have a problem with the client part.
In the client part, I start by receiving messages from the server but when I want to send a message to the server, I have a problem - how can I get the server socket that I can use as first parameter in the send function?
Function: int connect (int socket, struct sockaddr *addr, socklen_t length)
this is a part of the client code :
#if defined (WIN32)
#include <winsock2.h>
typedef int socklen_t;
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#endif
#include <stdio.h>
#include <stdlib.h>
#define PORT 23
int main(void)
{
#if defined (WIN32)
WSADATA WSAData;
int erreur = WSAStartup(MAKEWORD(2,2), &WSAData);
#else
int erreur = 0;
#endif
SOCKET csock;
SOCKADDR_IN sin;
char buffer[32] = "";
if(!erreur)
{
csock = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
if(connect(csock, (SOCKADDR*)&sin, sizeof(sin)) != SOCKET_ERROR)
{
//
if(recv(csock, buffer, 32, 0) != SOCKET_ERROR)
printf("Recu : %s\n", buffer);
char buffer1[32] = "how are you !";
send( ?sock?, buffer1, 32, 0); // here is my problem , so what is the first parameter(?sock?) in the "send" function that i need to send message to the server ?
.......
......
.....
You already have it: it is your csock variable. TCP Connections are established both ways. When you connected to your sever you got value for your csock: you can use it for both sending and receiving data.