Код: Выделить всё
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
char message[30];
char buf[sizeof(message)];
int sock;
void * f_zap()
{
for (;;)
{
scanf("%s",message);
send(sock, message, sizeof(message), 0);
}
}
void * f_rec()
{
for(;;)
{
printf("%s",">>");
recv(sock, buf, sizeof(message), 0);
printf(buf);
}
}
int main()
{
int id1, id2, result;
pthread_t thread1, thread2;
struct sockaddr_in addr;
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
perror("socket");
exit(1);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(3425); // или любой другой порт...
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("connect");
exit(2);
}
id1 = 1;
result = pthread_create(&thread1, NULL, f_zap, &id1);
id2 = 2;
result = pthread_create(&thread2, NULL, f_rec, &id2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
close(sock);
return 0;
}
исправил немного. такая проблема - отправляю сообщение - а пока enter не нажму его не выводит на экран. сервер - эхо. ?