C 客户端/服务器套接字错误

标签 c multithreading sockets client

我正在用 C 语言编写一个基本的聊天程序。我已经为其构建了框架,但在修复我遇到的用户名错误之前我无法继续进行。我的客户端和服务器正在通信,但是当我输入用户名时,服务器显示之前输入的用户名。

EX:客户端程序输入Joe:服务器不显示任何内容

客户端程序输入Tom:服务器显示“用户名输入:Joe”

客户端程序输入Rob:服务器显示“用户名输入:Tom”

这是客户端.c

#include <sys/socket.h>
#include <pthread.h>
#include <time.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#define SA struct sockaddr
#define MAXLINE 4096
//void leave(int sig); /* Things to do after signal handler is called*/ 

void error(const char *msg)
{
perror(msg);
exit(0);
}

/*Routine to send data*/ 
static void *do_send(void *arg) 
{ 
int fd = *(int*)(arg); //Socket File Descriptor
char payload[512];
for(; ;) { 

    scanf("%s\r\n\n", payload);
    write(fd, payload, sizeof(payload));

    //Choose an available client’s username and send it to the server 
    //if ( the peer is ready to chat) 
    //for(; ;) {
        //Code to send msg to its peer  
    //}
}
pthread_exit((void *)0); 
}

/*Routine to receive data*/ 
static void *do_recv(void *arg) 
{

int fd = *(int*)(arg);
char payload[512];
for(; ;) { 
    //Code to receive data 
    read(fd, payload, sizeof(payload));
    //Print data 
    printf("%s\n", payload);
} 
pthread_exit((void *)0); 
}

int main(int argc, char **argv) 
{   
//Declare variables
int sockfd, n;
char recvline[MAXLINE+1];
struct sockaddr_in servaddr;
int status;
pthread_t chld_thr1, chld_thr2;

//Create socket. 
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
    error("Socket error");
}

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(1024);
//Send a connection request to server 
if(connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
{
    error("Connect error");
}
//Register username
/* Create a thread to send data */ 
pthread_create(&chld_thr1, NULL, do_send, (void *)&sockfd);
/* Create a thread to receive data */ 
pthread_create(&chld_thr2, NULL, do_recv, (void *)&sockfd);
//pthread_join(do_send, NULL);
//Close connection and exit

//Loops so program doesn't close
for(; ;){

}
}

这是服务器.c

#include <sys/socket.h>
#include <pthread.h>
#include <time.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Function prototypes and global variables */

#define SA  struct sockaddr
#define LISTENQ 1024

/*Function executed by the new thread */
static void *do_child(void *arg) 
{
//Declare variables 
int fd = *(int*)(arg); //Socket Descriptor
char payload[512];
int list = 1;
int chat = 2;
int exit = 3;
int bytesRead = 0;

//Initial verification and registration codes
strcpy(payload, "Please enter your username:"); 
write(fd, payload, sizeof(payload));

for(; ;) {
    //Receive messages from client 
    //Code to receive data 
    bytesRead = read(fd, payload, sizeof(payload));
    //Print data
    if (bytesRead > 0){
        printf("Username entered: %s\n", payload);
    }

    //Check the msg_type of the message
    //if(m1.msg_type == list)
        //Send the list of available clients 
    //if(m1.msg_type ==chat)
        //Forward messages to the peer that a client wants to chat with you 
        //if(m1.msg_type == chatMessage)
        //Forward messages to its peer
    //if(m1.msg_type == exit) 

        //Remove the client from the client list. Close the client connection
}
pthread_exit((void *)0);
}

int main(int argc, char *argv[]) {
//Declare Variables
int listenfd, connfd; //Socket ID's
struct sockaddr_in servaddr;
pthread_t chld_thr;
char payload[512];

//Create Socket
listenfd = socket(AF_INET, SOCK_STREAM, 0);

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

servaddr.sin_port = htons(1024);

//Bind to the Address and Port Number Pair
bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

//Listen
listen(listenfd, LISTENQ);

for(; ;) {
   connfd = accept(listenfd, (SA *) NULL, NULL);
   if (connfd > 0){
        strcpy(payload, "Connection Accepted");
        printf("%s\n", payload);

        /*Create a new thread to handle the connections. */ 
        pthread_create(&chld_thr, NULL, do_child, (void *)&connfd);
    }
    else
        printf("%s\n", "No connection could be made.");
} 
//Close listen connection and exit.
}

最佳答案

简单....

更改: scanf("%s\r\n\n", 负载);

至: scanf("%s", 负载);

关于C 客户端/服务器套接字错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26264953/

相关文章:

c - 相当于 GLib 中的 open(O_WRONLY | O_CREAT)?

c - c中的字符串读取

c - 使用 strncmp C 风格的字符串函数

windows - VB - 以隐式方式链接 DLL

javascript - QWebEngine & QWebChannel : transport object `qt.webChannelTransport` disappeared after page reload

c - 删除函数在 C 中不适用于特定文件但适用于其他文件

linux - 除了内核线程堆栈之外,内核是否有自己的堆栈?

java - 安卓开发 : How do I integrate threads in this?

java - 通过套接字发送多个图像

c++ - Qt:服务器从套接字接收数据