This question already has an answer here:
Bind return error 88
(1 answer)
Closed 5 years ago.
I was searching for an answer for my issue though nothing relevant came up.
I'm writing a very simple code for running a server on a virtual machine (VBOX) running ubuntu 14.04.
I turned off my firewall and my anti-virus program (read that it might be related)
I rechecked (and looked for different ports) that the port is not in use but keep on receiving return value of -1 to the bind() function with errno 88 (socket operation on non-socket).
I'm running the server on port 7777.
Also tried running this code on my host
Can someone suggest what I am doing wrong?
p.s also checked the code with valgrind for memory leaks but it looks fine.
the code is as follows:
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <iostream>
#include <fstream>
#include <strings.h>
#include <stdlib.h>
#include <string>
#include <pthread.h>
#include <errno.h>
using namespace std;
#define NUM_OF_THREADS 3
static int connFd;
void *task1(void *);
/*
*
*/
int main(int argc, char** argv) {
int pid, portNo, listenFd;
socklen_t len; // store the size of the address
bool loop = false;
struct sockaddr_in svrAdd, clntAdd;
pthread_t threadArr[NUM_OF_THREADS];
if (argc < 2){
cerr << "ERROR: ./server <port>" << endl;
return 0;
}
portNo = atoi(argv[1]); // TODO verify the port is between 1024 and 65535
//Create the socket
if (listenFd = socket(AF_INET, SOCK_STREAM, 0) < 0){
cerr << "ERROR: cannot open socket." << endl;
return 0;
}
bzero((char*)&svrAdd, sizeof(svrAdd));
svrAdd.sin_family = AF_INET;
svrAdd.sin_addr.s_addr = INADDR_ANY;
svrAdd.sin_port = htons(portNo);
cout << "port number is : " << portNo << endl;
//bind socket
int bound = bind(listenFd, (struct sockaddr *)&svrAdd, sizeof(svrAdd));
if ( bound < 0 ){
cerr << "ERROR: cannot bind, error number: " << errno << endl;
return 0;
}
listen(listenFd, 5);
len = sizeof(clntAdd);
int noTread = 0;
while(noTread < 3){
cout << "Listening..." << endl;
if (connFd = accept(listenFd, (struct sockaddr*)&clntAdd, &len)<0){
cerr << "ERROR: cannot accept connection" << endl;
return 0;
}
else{
cout << "Connection successful" << endl;
}
pthread_create(&threadArr[noTread], NULL, task1, NULL);
noTread++;
}
for (int i=0; i < 3; i++){
pthread_join(threadArr[i], NULL);
}
/*int sockfd, newsockfd, portno, clilen, n;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
if (argc < 2){
fprintf(stderr, "ERROR, no port provided");
exit(1);
}
if(sockfd = socket(AF_INET, SOCK_STREAM, 0) < 0){
error("ERROR opening socket");
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
serv_addr.sin_addr.s_addr - INADDR_ANY;
if(bind(sockfd, (struct sockaddr*)&))*/
return 0;
}
void *task1(void *dummyPt){
cout << "Thread number: " << pthread_self() << endl;
char test[300];
bzero(test, 301);
bool loop = false;
while (!false){
bzero(test, 301);
read(connFd, test, 300);
string tester(test);
cout << "\n\t\t TESTER = " << tester << endl;
if(tester == "exit"){
break;
}
}
cout << "\n Closing thread and conn" << endl;
close(connFd);
}
The output of the execution:
ERROR: cannot bind, error number: 88
port number is : 7777
RUN SUCCESSFUL (total time: 162ms)
Please help,
Thanks.
if (listenFd = socket(AF_INET, SOCK_STREAM, 0) < 0){
Precedence problem. The result of this condition is to assign zero or 1 to listenFd. Try this:
if ((listenFd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
Related
I'm using ubuntu 20.04 now. I have text file in client document. The server can listen the port and I can sen the file but when I open it, there is no text.
client code;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
const int BUFSIZE = 4096;
int main(int argc, char *argv[]) {
int client_socket;
struct sockaddr_in server_address;
char buffer[BUFSIZE];
// Create a socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
std::cerr << "Failed to create socket" << std::endl;
return 1;
}
// Set up the server address
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(12345);
if (inet_aton("127.0.0.1", &server_address.sin_addr) == 0) {
std::cerr << "Invalid address" << std::endl;
return 1;
}
// Connect to the server
if (connect(client_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
std::cerr << "Failed to connect to server" << std::endl;
return 1;
}
std::cout << "Connected to server at " << inet_ntoa(server_address.sin_addr) << ":" << ntohs(server_address.sin_port) << std::endl;
// Send the file
std::ifstream input_file("file_to_send.txt", std::ios::binary); //
int bytes_sent;
while (input_file.read(buffer, BUFSIZE)) {
bytes_sent = send(client_socket, buffer, input_file.gcount(), 0);
if (bytes_sent < 0) {
std::cerr << "Failed to send data" << std::endl;
break;
}
}
input_file.close();
close(client_socket);
std::cout << "File 'file_to_send.txt' sent" << std::endl;
return 0;
}
server code;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
const int BUFSIZE = 4096;
int main(int argc, char *argv[]) {
int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
socklen_t client_address_len;
char buffer[BUFSIZE];
// Create a socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
std::cerr << "Failed to create socket" << std::endl;
return 1;
}
// Set up the server address
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(12345);
// Bind the socket to the server address
if (bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address)) < 0) {
std::cerr << "Failed to bind socket" << std::endl;
return 1;
}
// Listen for incoming connections
if (listen(server_socket, 5) < 0) {
std::cerr << "Failed to listen on socket" << std::endl;
return 1;
}
std::cout << "Listening for incoming connections on port 12345.." << std::endl;
// Accept an incoming connection
client_address_len = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr *) &client_address, &client_address_len);
if (client_socket < 0) {
std::cerr << "Failed to accept connection" << std::endl;
return 1;
}
std::cout << "Accepted connection from " << inet_ntoa(client_address.sin_addr) << std::endl;
// Receive the file
std::ofstream output_file("received_file.txt", std::ios::binary);
int bytes_received;
std::cout << "Bytes received: " << bytes_received << std::endl;
while( (bytes_received = recv(client_socket, buffer, BUFSIZE,0)))>0{
std::cout << "Bytses recieved are not null." << std::endl;
output_file.write(buffer , bytes_received); }
output_file.close();
close(client_socket);
close(server_socket);
std::cout << "File received and saved as 'received_file.txt'" << std::endl;
return 0;
}
I added sys/socket.h library but I think there is a problem in line 65. I ran the server and client. On terminaL, I see "Connected to server at" and "File 'file_to_send.txt' sent". And for server, I see "Listening for incoming connections on port","Accepted connection from ", "Bytes received: 0" and "File received and saved as 'received_file.txt'". "Hello world" is written in the text file I'm new to this topic so I don't know what to do properly. How can I handle this? Thank you.
I'm trying to construct a simple UDP network program in C++ to establish a real-time data communication platform between two computers in my company.
The below code is for Server (receiver), and I successfully tested the network self-communication (IP='127.0.0.1').
However, if I change the IP number corresponding to another computer (147.47.42.50), I face a binding failure error.
When I type 'ping' in cmd, it successfully returns responses.
Is there any incorrect logic in my program? and is there any way to debug this problem?
#include <stdio.h>
#include <iostream>
#include <winsock2.h>
#include <windows.h>
#pragma comment (lib,"ws2_32.lib")
#define BUFFER_SIZE 1024
using namespace std;
void main(void)
{
WSADATA wsaData;
SOCKET ServerSocket;
SOCKADDR_IN ServerInfo;
SOCKADDR_IN FromClient;
int FromClient_Size;
int Recv_Size;
int Send_Size;
int Count;
char Buffer[BUFFER_SIZE];
short ServerPort = 6000;
if (WSAStartup(0x202, &wsaData) == SOCKET_ERROR)
{
cout << "WinSock initialization fail. " << endl;
WSACleanup();
}
memset(&ServerInfo, 0, sizeof(ServerInfo));
memset(&FromClient, 0, sizeof(FromClient));
memset(Buffer, 0, BUFFER_SIZE);
ServerInfo.sin_family = AF_INET;
ServerInfo.sin_addr.s_addr = inet_addr("147.47.42.50");
ServerInfo.sin_port = htons(ServerPort);
ServerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (ServerSocket == INVALID_SOCKET) //
{
cout << "Cannot create socket." << endl;
closesocket(ServerSocket);
WSACleanup();
exit(0);
}
if (bind(ServerSocket, (struct sockaddr*)&ServerInfo,
sizeof(struct sockaddr)) == SOCKET_ERROR)
{
cout << "Bind fail." << endl;
closesocket(ServerSocket);
WSACleanup();
exit(0);
}
while (1)
{
FromClient_Size = sizeof(FromClient);
Recv_Size = recvfrom(ServerSocket, Buffer, BUFFER_SIZE, 0,
(struct sockaddr*)&FromClient, &FromClient_Size);
if (Recv_Size < 0)
{
cout << "recvfrom() error!" << endl;
exit(0);
}
cout << "Receive! client IP: " << inet_ntoa(FromClient.sin_addr) << endl;
cout << "Data: " << Buffer << endl;
}
closesocket(ServerSocket);
WSACleanup();
}
I'm trying to set up a server using sockets, and the port I wish to be listened to seems to not be working properly. When I run the netstat -nlp command the port will not show up.
int openListenFd(int port){
int socketDesc, newSocket;
int opt = 1;
struct sockaddr_in server, client;
string message;
cout << "The port is " << port << endl;
int myPort = 3207;
socketDesc = socket(AF_INET, SOCK_STREAM, 0);
if(socketDesc == -1){
cout << "ERROR CREATING SOCKET DESCRIPTOR" << endl;
exit(EXIT_FAILURE);
}
if(setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))){
cout << "Setsocket error" << endl;
exit(EXIT_FAILURE);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(myPort);
if(bind(socketDesc, (struct sockaddr *)&server, sizeof(server)) < 0){
cout << "BIND FAILED" << endl;
exit(EXIT_FAILURE);
}
cout << "Bind finished" << endl; //Page 982
if(listen(socketDesc,10) < 0){
close(socketDesc);
}
return socketDesc;
}
This should return the file descriptor for the connection, and it does not error at any point. It later gets to the accept() call where it simply waits, likely because no client can connect to it since the port isn't working.
Works as expected for me. Test program:
#include <string>
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
int openListenFd() {
int socketDesc;
int opt = 1;
struct sockaddr_in server;
string message;
int port = 3207;
cout << "The port is " << port << endl;
socketDesc = socket(AF_INET, SOCK_STREAM, 0);
if(socketDesc == -1){
cout << "ERROR CREATING SOCKET DESCRIPTOR" << endl;
exit(EXIT_FAILURE);
}
if(setsockopt(socketDesc, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))){
cout << "Setsocket error" << endl;
exit(EXIT_FAILURE);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(port);
if(bind(socketDesc, (struct sockaddr *)&server, sizeof(server)) < 0){
cout << "BIND FAILED" << endl;
exit(EXIT_FAILURE);
}
cout << "Bind finished" << endl; //Page 982
if(listen(socketDesc,10) < 0){
close(socketDesc);
}
return socketDesc;
}
int main() {
int fd = openListenFd();
int rc = system("netstat -tlnp");
return !(fd && !rc);
}
Outputs:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:3207 0.0.0.0:* LISTEN 31807/test
I've discovered that it is indeed not the code that was the issue. The reason the port was not showing up was that I was running it on a university Linux server, which had a virtual machine on it. As a result, the loopback address of 127.0.0.1 was not working properly. This probably could be fixed if I had administrator privileges on it, however, that is not the case.
The solution was to download ubuntu and run it within my own computer, thus making the loopback address valid.
I have a client and server set up to talk to each other. But every time I try to echo back to the client the socket seems to have disconnected. Much of the code is adapted from a sockets tutorial over at yolinux. Also, I'm running this remotely over ssh.
Client:
#include <cerrno>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/select.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <string>
#include <sstream>
using namespace std;
main(int argc, char *argv[])
{
if (argc != 3) {
cout << "exiting\n";
exit(EXIT_FAILURE);
}
struct sockaddr_in remoteSocketInfo;
struct hostent *hPtr;
int socketHandle;
char *remoteHost = argv[1];
int portNumber = atoi(argv[2]);
cout << "Welcome!\n";
// create socket
if ((socketHandle = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0)
{
cout << "Socket creation failed.\n";
close(socketHandle);
exit(EXIT_FAILURE);
}
cout << "Socket created!\n";
bzero(&remoteSocketInfo, sizeof(sockaddr_in)); // Clear structure memory
if ((hPtr = gethostbyname(remoteHost)) == NULL)
{
cerr << "System DN name resolution not configured properly.\n";
cerr << "Error number: " << ECONNREFUSED << endl;
exit(EXIT_FAILURE);
}
// Load system information for remote socket server into socket data structures
memcpy((char*)&remoteSocketInfo.sin_addr, hPtr->h_addr, hPtr->h_length);
remoteSocketInfo.sin_family = AF_INET;
remoteSocketInfo.sin_port = htons((u_short)portNumber); // set port number
if (connect(socketHandle, (struct sockaddr *)&remoteSocketInfo, sizeof(sockaddr_in)) < 0) {
cout << "connection failed\n";
close(socketHandle);
exit(EXIT_FAILURE);
}
cout << "Connected!\n";
string input;
int message;
while (1) {
cout << "Please indicate rotation amount:";
cin >> input;
if (input == "exit") {
close(socketHandle);
break;
}
char buf[input.length()+1];
const char *conv_input = input.c_str();
strcpy(buf, conv_input);
int bytes_sent = 0;
if ( (bytes_sent = send(socketHandle, buf, strlen(buf)+1, 0)) < 0) {
char buffer[256];
char * errorMessage = strerror_r( errno, buffer, 256);
cout << errorMessage << endl;
close(socketHandle);
exit(EXIT_FAILURE);
}
cout << "bytes sent: " << bytes_sent << endl;
int rc;
char buf2[input.length()+1];
rc = recv(socketHandle, buf2, strlen(buf)+1, 0);
buf[rc] = (char)NULL; // Null terminate string
cout << "received: " << buf2 << endl;
cout << "bytes received: " << rc << endl;
}
close(socketHandle);
}
Server:
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdlib.h>
#include <strings.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <cstring>
#define MAXHOSTNAME 256
using namespace std;
main(int argc, char *argv[])
{
if (argc != 2) {
cout << "not enough arguments, ex: ./CaesarCipherServer 9876\n";
exit(EXIT_FAILURE);
}
struct sockaddr_in socketInfo;
char sysHost[MAXHOSTNAME+1]; // Hostname of this computer we're running on
struct hostent *hPtr;
int portNumber = atoi(argv[1]);
int sock;
bzero(&socketInfo, sizeof(sockaddr_in)); // Clear structure memory
// Get system information
gethostname(sysHost, MAXHOSTNAME); // Get this computer's hostname
if ((hPtr = gethostbyname(sysHost)) == NULL)
{
cerr << "System hostname misconfigured." << endl;
exit(EXIT_FAILURE);
}
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
close(sock);
exit(EXIT_FAILURE);
}
// Load system info into socket data structures
socketInfo.sin_family = AF_INET;
socketInfo.sin_addr.s_addr = htonl(INADDR_ANY); // Use any addr available
socketInfo.sin_port = htons(portNumber); // Set port number
// Bind the socket to a local socket address
if (bind(sock, (struct sockaddr *) &socketInfo, sizeof(socketInfo)) < 0)
{
close(sock);
perror("bind");
exit(EXIT_FAILURE);
}
cout << "listening for initial connection \n";
listen(sock, 1);
int sockConn;
if ((sockConn = accept(sock, NULL, NULL)) < 0)
{
exit(EXIT_FAILURE);
} else {
cout << "connection accepted!\n";
}
int rc = 0;
char buf[512];
cout << "about to receive message... \n";
// rc is number of chars returned
rc = recv(sockConn, buf, 512, 0);
buf[rc] = (char)NULL; // Null terminate string
cout << "received: " << buf << endl;
cout << "rc: " << rc << endl;
int bytes_sent;
if ((bytes_sent = send(sock, buf, rc, MSG_NOSIGNAL)) < 0) {
cout << "error sending\n";
close(sock);
exit(EXIT_FAILURE);
}
cout << "bytes sent: " << bytes_sent << endl;
close(sock);
}
Client Output:
./CaesarCipherClient cs-ssh 9876
Welcome!
Socket created!
socket handle : 3
Connected!
Please indicate rotation amount:5
bytes sent: 2
received:
bytes received: 0
Please indicate rotation amount:
Server Output:
./CaesarCipherServer 9876
listening for initial connection
connection accepted!
about to receive message...
received: 5
rc: 2
error sending
If the MSG_NOSIGNAL flag isn't specified, the server crashes at send(), which means the socket has disconnected at the other end. Why would the socket consistently disconnect after a send()/recv() pair?
I apologize for any poor readability/style/pure stupidity in my submission.
Thank you for your help!
In your server, you are using:
if ((bytes_sent = send(sock, buf, rc, MSG_NOSIGNAL)) < 0) {
cout << "error sending\n";
close(sock);
exit(EXIT_FAILURE);
}
Here, sock is the listening socket, not the accepted client socket. You need to replace sock with sockCon instead (which you are using in your recv() function call, and that is working).
I have to create a simple tcp server that accepts some commands from the server and responds to them in a certain way. I started with just a simple thing in which server accepts anything from client as command and responds "In response to your command" to client 10 times as answer. But even this is not working. Please help.
server.cpp
# include <string.h>
# include <unistd.h>
# include <stdio.h>
# include <netdb.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <iostream>
# include <fstream>
# include <string.h>
# include <stdlib.h>
# include <string>
# include <pthread.h>
# include <dirent.h>
using namespace std;
void *task1(void *);
static int connFd;
int main(int argc, char* argv[])
{
int pId, portNo, listenFd;
socklen_t len; //store size of the address
bool loop = false;
struct sockaddr_in svrAdd, clntAdd;
pthread_t threadA[3];
if (argc < 2)
{
cerr << "Syntax : ./server <port>" << endl;
return 0;
}
portNo = atoi(argv[1]);
if((portNo > 65535) || (portNo < 2000))
{
cerr << "Please enter a port number between 2000 - 65535" << endl;
return 0;
}
//create socket
listenFd = socket(AF_INET, SOCK_STREAM, 0);
if(listenFd < 0)
{
cerr << "Cannot open socket" << endl;
return 0;
}
bzero((char*) &svrAdd, sizeof(svrAdd));
svrAdd.sin_family = AF_INET;
svrAdd.sin_addr.s_addr = INADDR_ANY;
svrAdd.sin_port = htons(portNo);
//bind socket
if(bind(listenFd, (struct sockaddr *)&svrAdd, sizeof(svrAdd)) < 0)
{
cerr << "Cannot bind" << endl;
return 0;
}
listen(listenFd, 5);
len = sizeof(clntAdd);
int noThread = 0;
while (noThread < 3)
{
cout << "Listening" << endl;
//this is where client connects. svr will hang in this mode until client conn
connFd = accept(listenFd, (struct sockaddr *)&clntAdd, &len);
if (connFd < 0)
{
cerr << "Cannot accept connection" << endl;
return 0;
}
else
{
cout << "Connection successful" << endl;
}
pthread_create(&threadA[noThread], NULL, task1, NULL);
noThread++;
}
for(int i = 0; i < noThread; i++)
{
pthread_join(threadA[i], NULL);
}
cout << "All threads have joined" << endl;
}
void *task1 (void *dummyPt)
{
cout << "Thread No: " << pthread_self() << endl;
char test[300];
char outbuf[300];
bzero(outbuf, 301);
bzero(test, 301);
bool loop = false;
int recvdBytes = 0;
strcpy(outbuf, "Enter your username: \n");
write(connFd, outbuf, strlen(outbuf));
bzero(outbuf, 301);
read(connFd, test, 300);
string uname(test);
strcpy(outbuf, "You are logged in\n");
write(connFd, outbuf, strlen(outbuf));
bzero(outbuf, 301);
while(!loop)
{
bzero(test, 301);
read(connFd, test, 300);
string tester (test);
cout << tester << endl;
if(tester == "exit")
break;
for (int i=0; i<10; ++i)
{
strcpy(outbuf, "In response to your command");
write(connFd, outbuf, strlen(outbuf));
bzero(outbuf, 301);
}
}
cout << "\nClosing thread and conn" << endl;
close(connFd);
}
client.cpp
# include <string.h>
# include <cstring>
# include <unistd.h>
# include <stdio.h>
# include <netdb.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <iostream>
# include <fstream>
# include <sstream>
# include <iomanip>
# include <strings.h>
# include <stdlib.h>
# include <string>
# include <time.h>
# include <vector>
using namespace std;
int main (int argc, char* argv[])
{
int sockFd, portNo;
bool loop = false;
struct sockaddr_in svrAdd;
struct hostent *server;
char inbuf[300];
int recvdBytes=0;
if(argc < 3)
{
cerr << "Syntax : ./client <host name> <port>"<<endl;
return 0;
}
portNo = atoi(argv[2]);
if((portNo > 65535) || (portNo < 2000))
{
cerr << "Please enter port number between 2000 - 65535"<<endl;
return 0;
}
//create client skt
sockFd = socket(AF_INET, SOCK_STREAM, 0);
if(sockFd < 0)
{
cerr << "Cannot open socket" << endl;
return 0;
}
server = gethostbyname(argv[1]);
if(server == NULL)
{
cerr << "Host does not exist" << endl;
return 0;
}
bzero((char *) &svrAdd, sizeof(svrAdd));
svrAdd.sin_family = AF_INET;
bcopy((char *) server -> h_addr, (char *) &svrAdd.sin_addr.s_addr, server -> h_length);
svrAdd.sin_port = htons(portNo);
int checker = connect(sockFd,(struct sockaddr *) &svrAdd, sizeof(svrAdd));
if (checker < 0)
{
cerr << "Cannot connect!" << endl;
return 0;
}
recvdBytes = read(sockFd, inbuf, 300);
cout << inbuf;
bzero(inbuf, 300);
cin >> inbuf;
write(sockFd, inbuf, strlen(inbuf)); // sending my user name to server
bzero(inbuf, 300);
read(sockFd, inbuf, 300); // recieving the message from server
cout << inbuf;
bzero(inbuf, 300);
// send commands to server
for(;;)
{
char s[300];
//cin.clear();
//cin.ignore(256, '\n');
cout << "Enter command: ";
bzero(s, 301);
cin.getline(s, 300);
write(sockFd, s, strlen(s));
// recieve data in response to your command
while(read(sockFd, inbuf, 300))
{
if(strcmp(inbuf, "done") == 0) // display the output
break;
cout << inbuf;
bzero(inbuf, 300);
}
}
}
Usual problems.
connFD should not be static. It should be a local variable in both main() and task1(), and it should be communicated to the started thread somehow. Otherwise the next client will clobber it and break the first client.
You aren't error-testing listen().
You're ignoring the result of read() everywhere. You have to store it into a variable and check it for -1, zero, or a positive value: only in the positive case should the loop continue; and the positive value should be used to limit the number of bytes in the buffer you regard as having been received.
You are ignoring the result of write() everywhere.
So at present there is no evidence that anything has been sent anywhere by anybody.