Şimdi Ara

YARDIM: socket ile chat??

Daha Fazla
Bu Konudaki Kullanıcılar: Daha Az
1 Misafir - 1 Masaüstü
5 sn
4
Cevap
0
Favori
873
Tıklama
Daha Fazla
İstatistik
  • Konu İstatistikleri Yükleniyor
0 oy
Öne Çıkar
Sayfa: 1
Giriş
Mesaj
  • Arkadaşlar socket programlama ile çok kullanıcılı chat programı yazmaya çalışıyorum. Fakat programda bir kullanıcı mesaj yazmadan diğeri yazamıyor. Yani hep sırayla yazışmak gerekiyor, üstüste yazılamıyor. Bu sorunu nasıl çözebilirim?
    kodlar şöyle:

    Client.c
    //-------------
    #include <winsock.h>
    #include <winsock2.h>
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #define DEFAULT_PORT 5150

    int g_port = DEFAULT_PORT; // Port on server to connect to
    char g_szServerName[128] = "10.0.1.37"; //"KRANDAXP"; // Server to connect to

    int main(void)
    {
    WSADATA wsaData; // if this doesn't work
    //WSAData wsaData; // then try this instead

    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
    fprintf(stderr, "WSAStartup failed.\n");
    exit(1);
    }
    WSADATA wsd;
    SOCKET sClient;
    int ret, ch;
    struct sockaddr_in server;
    struct hostent *host = NULL;
    char buf[1024];
    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) {
    printf("Failed to load Winsock library!\n");
    getch();
    return 1;
    }

    //
    // Create the socket, and attempt to connect to the server
    //

    sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sClient == INVALID_SOCKET) {
    printf("socket() failed: %d\n", WSAGetLastError());
    getch();
    return 1;
    }
    server.sin_family = AF_INET;
    server.sin_port = htons(g_port);
    server.sin_addr.s_addr = inet_addr(g_szServerName);

    //
    // If the supplied server address wasn't in the form
    // "aaa.bbb.ccc.ddd" it's a hostname, so try to resolve it
    //

    if (server.sin_addr.s_addr == INADDR_NONE) {
    host = gethostbyname(g_szServerName);
    if (host == NULL) {
    printf("Unable to resolve server: %s\n", g_szServerName);
    getch();
    return 1;
    }
    CopyMemory(&server.sin_addr, host->h_addr_list[0], host->h_length);
    }
    if (connect(sClient, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) {
    printf("connect() failed: %d\n", WSAGetLastError());
    getch();
    return 1;
    }
    printf("\a\a \n\t ---Yeni Baslayanlar icin Chat 1.0--- \n\t--A program by B&M Software ... Licenced--\n\n");
    for (;;) {
    buf[0]='\0';
    printf("Me: ");
    fgets(buf, sizeof(buf)+1, stdin);
    if((send(sClient, buf, sizeof(buf), 0)) <= 0){ printf("Baglanti Koptu"); getch(); return 1; }
    if((recv(sClient, buf, sizeof(buf), 0)) <= 0){ printf("Baglanti Koptu"); getch(); return 1; }
    printf("Sunucu diyor ki: %s", buf);
    }

    closesocket(sClient);

    WSACleanup();
    getch();
    return 0;
    }

    //------------------------------------------------------------------------------
    server.c
    //-------------
    #include <winsock.h>
    #include <winsock2.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <conio.h>
    #include <time.h>
    #define DEFAULT_PORT 5150

    int g_port = DEFAULT_PORT;
    char g_szAddress[128];
    int main(void)
    {
    WSADATA wsd;
    SOCKET sListen,sClient;
    int addrSize;
    struct sockaddr_in local, client;
    char buffer[1024],buffer2[1024];

    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) {
    printf("Failed to load Winsock!\n");
    return 1;
    }

    sListen = socket(AF_INET, SOCK_STREAM, 0);

    if (sListen == SOCKET_ERROR) {
    printf("socket() failed: %d\n", WSAGetLastError());
    return 1;
    }

    local.sin_addr.s_addr = htonl(INADDR_ANY);
    local.sin_family = AF_INET;
    local.sin_port = htons(g_port);

    if (bind(sListen, (struct sockaddr *)&local,
    sizeof(local)) == SOCKET_ERROR) {
    printf("Dinleme Hatasi: %d\n", WSAGetLastError());
    return 1;
    }

    listen(sListen, 8);
    printf("Dinleme Basladi, Yanit bekleniyor...\n");

    for (;;)
    {
    addrSize = sizeof(client);
    sClient = accept(sListen, (struct sockaddr *) &client, &addrSize);

    if (sClient == INVALID_SOCKET)
    {
    printf("Baglanti Kabulu Hatasi: %d\n", WSAGetLastError());
    break;
    }

    printf("istemci Kabul Edildi, istemci Adresi: %s:%d\n\a", inet_ntoa(client.sin_addr), ntohs(client.sin_port));

    for(;;)
    {
    for(unsigned long i=0;i<1000000000;i++){
    printf("x");
    buffer2[0]='\0';
    if((recv(sClient,buffer2, sizeof(buffer2), 0) == -1))
    {printf("x1");
    printf("Baglanti Koptu!!!");
    break;
    }
    printf("x2");
    if(buffer2)
    printf("\nistemci diyor ki: %s", buffer2);
    printf("x3");
    }

    printf("\nBen: ");
    buffer[0]='\0';
    fgets(buffer, sizeof(buffer), stdin);

    if (send(sClient,buffer, sizeof(buffer), 0) == -1){
    printf("Baglanti Koptu!!!");break;}

    }
    }
    closesocket(sListen);

    WSACleanup();

    return 0;
    }







  • merhaba bende soket programlamaya meraklıyım, bu kodu hangi derleyicide derledin, ben Dev-Cpp'da derlemeye çalıştım ama
    IPPRoto_TCP 'u tanımadı, neyapabilirim, tanımlamasını biliyorsan burayada yazarmısın, derleyici değiştirmek istemiyorum aslında...
    teşekkürler
  • benim yaptığım include ların yanında yaratmış olduğun projeye proje->proje ayarları->parametreler->kütüphane ekle'den libwsock32.a yı eklemelisin(devc / lib dizini altında bulunur). Bu arada sorunu hallettik.
  • ekledim olmadı, hatta sonra lib dizinindeki herşeyi ekledim ama yine olmadı:D
  • 
Sayfa: 1
- x
Bildirim
mesajınız kopyalandı (ctrl+v) yapıştırmak istediğiniz yere yapıştırabilirsiniz.